LCOV - code coverage report
Current view: top level - test/cctest - test-utils.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 93 97 95.9 %
Date: 2019-04-17 Functions: 11 11 100.0 %

          Line data    Source code
       1             : // Copyright 2011 the V8 project authors. All rights reserved.
       2             : // Redistribution and use in source and binary forms, with or without
       3             : // modification, are permitted provided that the following conditions are
       4             : // met:
       5             : //
       6             : //     * Redistributions of source code must retain the above copyright
       7             : //       notice, this list of conditions and the following disclaimer.
       8             : //     * Redistributions in binary form must reproduce the above
       9             : //       copyright notice, this list of conditions and the following
      10             : //       disclaimer in the documentation and/or other materials provided
      11             : //       with the distribution.
      12             : //     * Neither the name of Google Inc. nor the names of its
      13             : //       contributors may be used to endorse or promote products derived
      14             : //       from this software without specific prior written permission.
      15             : //
      16             : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      17             : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      18             : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      19             : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      20             : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      21             : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      22             : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      23             : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      24             : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      25             : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      26             : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      27             : 
      28             : #include <stdlib.h>
      29             : 
      30             : #include <vector>
      31             : 
      32             : #include "src/v8.h"
      33             : 
      34             : #include "src/api-inl.h"
      35             : #include "src/base/platform/platform.h"
      36             : #include "src/collector.h"
      37             : #include "src/conversions.h"
      38             : #include "test/cctest/cctest.h"
      39             : 
      40             : namespace v8 {
      41             : namespace internal {
      42             : 
      43       26644 : TEST(Utils1) {
      44             :   CHECK_EQ(-1000000, FastD2I(-1000000.0));
      45             :   CHECK_EQ(-1, FastD2I(-1.0));
      46             :   CHECK_EQ(0, FastD2I(0.0));
      47             :   CHECK_EQ(1, FastD2I(1.0));
      48             :   CHECK_EQ(1000000, FastD2I(1000000.0));
      49             : 
      50             :   CHECK_EQ(-1000000, FastD2I(-1000000.123));
      51             :   CHECK_EQ(-1, FastD2I(-1.234));
      52             :   CHECK_EQ(0, FastD2I(0.345));
      53             :   CHECK_EQ(1, FastD2I(1.234));
      54             :   CHECK_EQ(1000000, FastD2I(1000000.123));
      55             :   // Check that >> is implemented as arithmetic shift right.
      56             :   // If this is not true, then ArithmeticShiftRight() must be changed,
      57             :   // There are also documented right shifts in assembler.cc of
      58             :   // int8_t and intptr_t signed integers.
      59             :   CHECK_EQ(-2, -8 >> 2);
      60             :   CHECK_EQ(-2, static_cast<int8_t>(-8) >> 2);
      61             :   CHECK_EQ(-2, static_cast<int>(static_cast<intptr_t>(-8) >> 2));
      62             : 
      63             :   CHECK_EQ(-1000000, FastD2IChecked(-1000000.0));
      64             :   CHECK_EQ(-1, FastD2IChecked(-1.0));
      65             :   CHECK_EQ(0, FastD2IChecked(0.0));
      66             :   CHECK_EQ(1, FastD2IChecked(1.0));
      67             :   CHECK_EQ(1000000, FastD2IChecked(1000000.0));
      68             : 
      69             :   CHECK_EQ(-1000000, FastD2IChecked(-1000000.123));
      70             :   CHECK_EQ(-1, FastD2IChecked(-1.234));
      71             :   CHECK_EQ(0, FastD2IChecked(0.345));
      72             :   CHECK_EQ(1, FastD2IChecked(1.234));
      73             :   CHECK_EQ(1000000, FastD2IChecked(1000000.123));
      74             : 
      75             :   CHECK_EQ(INT_MAX, FastD2IChecked(1.0e100));
      76             :   CHECK_EQ(INT_MIN, FastD2IChecked(-1.0e100));
      77           5 :   CHECK_EQ(INT_MIN, FastD2IChecked(std::numeric_limits<double>::quiet_NaN()));
      78           5 : }
      79             : 
      80             : 
      81       26644 : TEST(BitSetComputer) {
      82             :   typedef BitSetComputer<bool, 1, kSmiValueSize, uint32_t> BoolComputer;
      83             :   CHECK_EQ(0, BoolComputer::word_count(0));
      84             :   CHECK_EQ(1, BoolComputer::word_count(8));
      85             :   CHECK_EQ(2, BoolComputer::word_count(50));
      86             :   CHECK_EQ(0, BoolComputer::index(0, 8));
      87             :   CHECK_EQ(100, BoolComputer::index(100, 8));
      88             :   CHECK_EQ(1, BoolComputer::index(0, 40));
      89             :   uint32_t data = 0;
      90             :   data = BoolComputer::encode(data, 1, true);
      91             :   data = BoolComputer::encode(data, 4, true);
      92             :   CHECK(BoolComputer::decode(data, 1));
      93             :   CHECK(BoolComputer::decode(data, 4));
      94             :   CHECK(!BoolComputer::decode(data, 0));
      95             :   CHECK(!BoolComputer::decode(data, 2));
      96             :   CHECK(!BoolComputer::decode(data, 3));
      97             : 
      98             :   // Lets store 2 bits per item with 3000 items and verify the values are
      99             :   // correct.
     100             :   typedef BitSetComputer<unsigned char, 2, 8, unsigned char> TwoBits;
     101             :   const int words = 750;
     102             :   CHECK_EQ(words, TwoBits::word_count(3000));
     103             :   const int offset = 10;
     104             :   Vector<unsigned char> buffer = Vector<unsigned char>::New(offset + words);
     105             :   memset(buffer.start(), 0, sizeof(unsigned char) * buffer.length());
     106        7505 :   for (int i = 0; i < words; i++) {
     107             :     const int index = TwoBits::index(offset, i);
     108        7500 :     unsigned char data = buffer[index];
     109        3750 :     data = TwoBits::encode(data, i, i % 4);
     110        3750 :     buffer[index] = data;
     111             :   }
     112             : 
     113        7505 :   for (int i = 0; i < words; i++) {
     114             :     const int index = TwoBits::index(offset, i);
     115        7500 :     unsigned char data = buffer[index];
     116        3750 :     CHECK_EQ(i % 4, TwoBits::decode(data, i));
     117             :   }
     118             :   buffer.Dispose();
     119           5 : }
     120             : 
     121             : 
     122       26644 : TEST(SNPrintF) {
     123             :   // Make sure that strings that are truncated because of too small
     124             :   // buffers are zero-terminated anyway.
     125             :   const char* s = "the quick lazy .... oh forget it!";
     126             :   int length = StrLength(s);
     127         655 :   for (int i = 1; i < length * 2; i++) {
     128             :     static const char kMarker = static_cast<char>(42);
     129         325 :     Vector<char> buffer = Vector<char>::New(i + 1);
     130         650 :     buffer[i] = kMarker;
     131         325 :     int n = SNPrintF(Vector<char>(buffer.start(), i), "%s", s);
     132         325 :     CHECK(n <= i);
     133         325 :     CHECK(n == length || n == -1);
     134         325 :     CHECK_EQ(0, strncmp(buffer.start(), s, i - 1));
     135         325 :     CHECK_EQ(kMarker, buffer[i]);
     136         325 :     if (i <= length) {
     137         165 :       CHECK_EQ(i - 1, StrLength(buffer.start()));
     138             :     } else {
     139         160 :       CHECK_EQ(length, StrLength(buffer.start()));
     140             :     }
     141             :     buffer.Dispose();
     142             :   }
     143           5 : }
     144             : 
     145             : 
     146             : static const int kAreaSize = 512;
     147             : 
     148             : 
     149      702405 : void TestMemMove(byte* area1,
     150             :                  byte* area2,
     151             :                  int src_offset,
     152             :                  int dest_offset,
     153             :                  int length) {
     154   719965125 :   for (int i = 0; i < kAreaSize; i++) {
     155   359631360 :     area1[i] = i & 0xFF;
     156   359631360 :     area2[i] = i & 0xFF;
     157             :   }
     158      702405 :   MemMove(area1 + dest_offset, area1 + src_offset, length);
     159      702405 :   memmove(area2 + dest_offset, area2 + src_offset, length);
     160      702405 :   if (memcmp(area1, area2, kAreaSize) != 0) {
     161             :     printf("MemMove(): src_offset: %d, dest_offset: %d, length: %d\n",
     162             :            src_offset, dest_offset, length);
     163           0 :     for (int i = 0; i < kAreaSize; i++) {
     164           0 :       if (area1[i] == area2[i]) continue;
     165           0 :       printf("diff at offset %d (%p): is %d, should be %d\n", i,
     166             :              reinterpret_cast<void*>(area1 + i), area1[i], area2[i]);
     167             :     }
     168           0 :     FATAL("memmove error");
     169             :   }
     170      702405 : }
     171             : 
     172             : 
     173       26644 : TEST(MemMove) {
     174           5 :   v8::V8::Initialize();
     175           5 :   byte* area1 = new byte[kAreaSize];
     176           5 :   byte* area2 = new byte[kAreaSize];
     177             : 
     178             :   static const int kMinOffset = 32;
     179             :   static const int kMaxOffset = 64;
     180             :   static const int kMaxLength = 128;
     181             :   STATIC_ASSERT(kMaxOffset + kMaxLength < kAreaSize);
     182             : 
     183         335 :   for (int src_offset = kMinOffset; src_offset <= kMaxOffset; src_offset++) {
     184       11055 :     for (int dst_offset = kMinOffset; dst_offset <= kMaxOffset; dst_offset++) {
     185     1410255 :       for (int length = 0; length <= kMaxLength; length++) {
     186      702405 :         TestMemMove(area1, area2, src_offset, dst_offset, length);
     187             :       }
     188             :     }
     189             :   }
     190           5 :   delete[] area1;
     191           5 :   delete[] area2;
     192           5 : }
     193             : 
     194             : 
     195       26644 : TEST(Collector) {
     196           5 :   Collector<int> collector(8);
     197             :   const int kLoops = 5;
     198             :   const int kSequentialSize = 1000;
     199             :   const int kBlockSize = 7;
     200          55 :   for (int loop = 0; loop < kLoops; loop++) {
     201          25 :     Vector<int> block = collector.AddBlock(7, 0xBADCAFE);
     202       50025 :     for (int i = 0; i < kSequentialSize; i++) {
     203       25000 :       collector.Add(i);
     204             :     }
     205         325 :     for (int i = 0; i < kBlockSize - 1; i++) {
     206         300 :       block[i] = i * 7;
     207             :     }
     208             :   }
     209           5 :   Vector<int> result = collector.ToVector();
     210           5 :   CHECK_EQ(kLoops * (kBlockSize + kSequentialSize), result.length());
     211          55 :   for (int i = 0; i < kLoops; i++) {
     212          25 :     int offset = i * (kSequentialSize + kBlockSize);
     213         325 :     for (int j = 0; j < kBlockSize - 1; j++) {
     214         300 :       CHECK_EQ(j * 7, result[offset + j]);
     215             :     }
     216          50 :     CHECK_EQ(0xBADCAFE, result[offset + kBlockSize - 1]);
     217       50025 :     for (int j = 0; j < kSequentialSize; j++) {
     218       50000 :       CHECK_EQ(j, result[offset + kBlockSize + j]);
     219             :     }
     220             :   }
     221             :   result.Dispose();
     222           5 : }
     223             : 
     224             : 
     225       26644 : TEST(SequenceCollector) {
     226             :   SequenceCollector<int> collector(8);
     227             :   const int kLoops = 5000;
     228             :   const int kMaxSequenceSize = 13;
     229             :   int total_length = 0;
     230       50005 :   for (int loop = 0; loop < kLoops; loop++) {
     231       25000 :     int seq_length = loop % kMaxSequenceSize;
     232             :     collector.StartSequence();
     233      324800 :     for (int j = 0; j < seq_length; j++) {
     234      149900 :       collector.Add(j);
     235             :     }
     236             :     Vector<int> sequence = collector.EndSequence();
     237      324800 :     for (int j = 0; j < seq_length; j++) {
     238      299800 :       CHECK_EQ(j, sequence[j]);
     239             :     }
     240       25000 :     total_length += seq_length;
     241             :   }
     242           5 :   Vector<int> result = collector.ToVector();
     243           5 :   CHECK_EQ(total_length, result.length());
     244             :   int offset = 0;
     245       50005 :   for (int loop = 0; loop < kLoops; loop++) {
     246       25000 :     int seq_length = loop % kMaxSequenceSize;
     247      324800 :     for (int j = 0; j < seq_length; j++) {
     248      299800 :       CHECK_EQ(j, result[offset]);
     249      149900 :       offset++;
     250             :     }
     251             :   }
     252             :   result.Dispose();
     253           5 : }
     254             : 
     255             : 
     256       26644 : TEST(SequenceCollectorRegression) {
     257             :   SequenceCollector<char> collector(16);
     258             :   collector.StartSequence();
     259           5 :   collector.Add('0');
     260             :   collector.AddBlock(
     261           5 :       i::Vector<const char>("12345678901234567890123456789012", 32));
     262             :   i::Vector<char> seq = collector.EndSequence();
     263           5 :   CHECK_EQ(0, strncmp("0123456789012345678901234567890123",
     264             :                       seq.start(), seq.length()));
     265           5 : }
     266             : 
     267             : 
     268       26644 : TEST(CPlusPlus11Features) {
     269             :   struct S {
     270             :     bool x;
     271             :     struct T {
     272             :       double y;
     273             :       int z[3];
     274             :     } t;
     275             :   };
     276             :   S s{true, {3.1415, {1, 2, 3}}};
     277             :   CHECK_EQ(2, s.t.z[1]);
     278             : 
     279             :   std::vector<int> vec{11, 22, 33, 44};
     280          10 :   vec.push_back(55);
     281          10 :   vec.push_back(66);
     282          35 :   for (auto& i : vec) {
     283          30 :     ++i;
     284             :   }
     285             :   int j = 12;
     286          35 :   for (auto i : vec) {
     287          30 :     CHECK_EQ(j, i);
     288          30 :     j += 11;
     289             :   }
     290           5 : }
     291             : 
     292             : }  // namespace internal
     293       79917 : }  // namespace v8

Generated by: LCOV version 1.10