v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-utils.cc
Go to the documentation of this file.
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 "v8.h"
31 
32 #include "cctest.h"
33 #include "platform.h"
34 #include "utils-inl.h"
35 
36 using namespace v8::internal;
37 
38 
39 TEST(Utils1) {
40  CHECK_EQ(-1000000, FastD2I(-1000000.0));
41  CHECK_EQ(-1, FastD2I(-1.0));
42  CHECK_EQ(0, FastD2I(0.0));
43  CHECK_EQ(1, FastD2I(1.0));
44  CHECK_EQ(1000000, FastD2I(1000000.0));
45 
46  CHECK_EQ(-1000000, FastD2I(-1000000.123));
47  CHECK_EQ(-1, FastD2I(-1.234));
48  CHECK_EQ(0, FastD2I(0.345));
49  CHECK_EQ(1, FastD2I(1.234));
50  CHECK_EQ(1000000, FastD2I(1000000.123));
51  // Check that >> is implemented as arithmetic shift right.
52  // If this is not true, then ArithmeticShiftRight() must be changed,
53  // There are also documented right shifts in assembler.cc of
54  // int8_t and intptr_t signed integers.
55  CHECK_EQ(-2, -8 >> 2);
56  CHECK_EQ(-2, static_cast<int8_t>(-8) >> 2);
57  CHECK_EQ(-2, static_cast<int>(static_cast<intptr_t>(-8) >> 2));
58 
59  CHECK_EQ(-1000000, FastD2IChecked(-1000000.0));
60  CHECK_EQ(-1, FastD2IChecked(-1.0));
61  CHECK_EQ(0, FastD2IChecked(0.0));
62  CHECK_EQ(1, FastD2IChecked(1.0));
63  CHECK_EQ(1000000, FastD2IChecked(1000000.0));
64 
65  CHECK_EQ(-1000000, FastD2IChecked(-1000000.123));
66  CHECK_EQ(-1, FastD2IChecked(-1.234));
67  CHECK_EQ(0, FastD2IChecked(0.345));
68  CHECK_EQ(1, FastD2IChecked(1.234));
69  CHECK_EQ(1000000, FastD2IChecked(1000000.123));
70 
71  CHECK_EQ(INT_MAX, FastD2IChecked(1.0e100));
72  CHECK_EQ(INT_MIN, FastD2IChecked(-1.0e100));
74 }
75 
76 
77 TEST(SNPrintF) {
78  // Make sure that strings that are truncated because of too small
79  // buffers are zero-terminated anyway.
80  const char* s = "the quick lazy .... oh forget it!";
81  int length = StrLength(s);
82  for (int i = 1; i < length * 2; i++) {
83  static const char kMarker = static_cast<char>(42);
84  Vector<char> buffer = Vector<char>::New(i + 1);
85  buffer[i] = kMarker;
86  int n = OS::SNPrintF(Vector<char>(buffer.start(), i), "%s", s);
87  CHECK(n <= i);
88  CHECK(n == length || n == -1);
89  CHECK_EQ(0, strncmp(buffer.start(), s, i - 1));
90  CHECK_EQ(kMarker, buffer[i]);
91  if (i <= length) {
92  CHECK_EQ(i - 1, StrLength(buffer.start()));
93  } else {
94  CHECK_EQ(length, StrLength(buffer.start()));
95  }
96  buffer.Dispose();
97  }
98 }
99 
100 
102  Vector<byte> dst,
103  int source_alignment,
104  int destination_alignment,
105  int length_alignment) {
106  memset(dst.start(), 0xFF, dst.length());
107  byte* to = dst.start() + 32 + destination_alignment;
108  byte* from = src.start() + source_alignment;
109  int length = OS::kMinComplexMemCopy + length_alignment;
110  OS::MemCopy(to, from, static_cast<size_t>(length));
111  printf("[%d,%d,%d]\n",
112  source_alignment, destination_alignment, length_alignment);
113  for (int i = 0; i < length; i++) {
114  CHECK_EQ(from[i], to[i]);
115  }
116  CHECK_EQ(0xFF, to[-1]);
117  CHECK_EQ(0xFF, to[length]);
118 }
119 
120 
121 
122 TEST(MemCopy) {
124  OS::SetUp();
125  const int N = OS::kMinComplexMemCopy + 128;
126  Vector<byte> buffer1 = Vector<byte>::New(N);
127  Vector<byte> buffer2 = Vector<byte>::New(N);
128 
129  for (int i = 0; i < N; i++) {
130  buffer1[i] = static_cast<byte>(i & 0x7F);
131  }
132 
133  // Same alignment.
134  for (int i = 0; i < 32; i++) {
135  TestMemCopy(buffer1, buffer2, i, i, i * 2);
136  }
137 
138  // Different alignment.
139  for (int i = 0; i < 32; i++) {
140  for (int j = 1; j < 32; j++) {
141  TestMemCopy(buffer1, buffer2, i, (i + j) & 0x1F , 0);
142  }
143  }
144 
145  // Different lengths
146  for (int i = 0; i < 32; i++) {
147  TestMemCopy(buffer1, buffer2, 3, 7, i);
148  }
149 
150  buffer2.Dispose();
151  buffer1.Dispose();
152 }
153 
154 
156  Collector<int> collector(8);
157  const int kLoops = 5;
158  const int kSequentialSize = 1000;
159  const int kBlockSize = 7;
160  for (int loop = 0; loop < kLoops; loop++) {
161  Vector<int> block = collector.AddBlock(7, 0xbadcafe);
162  for (int i = 0; i < kSequentialSize; i++) {
163  collector.Add(i);
164  }
165  for (int i = 0; i < kBlockSize - 1; i++) {
166  block[i] = i * 7;
167  }
168  }
169  Vector<int> result = collector.ToVector();
170  CHECK_EQ(kLoops * (kBlockSize + kSequentialSize), result.length());
171  for (int i = 0; i < kLoops; i++) {
172  int offset = i * (kSequentialSize + kBlockSize);
173  for (int j = 0; j < kBlockSize - 1; j++) {
174  CHECK_EQ(j * 7, result[offset + j]);
175  }
176  CHECK_EQ(0xbadcafe, result[offset + kBlockSize - 1]);
177  for (int j = 0; j < kSequentialSize; j++) {
178  CHECK_EQ(j, result[offset + kBlockSize + j]);
179  }
180  }
181  result.Dispose();
182 }
183 
184 
186  SequenceCollector<int> collector(8);
187  const int kLoops = 5000;
188  const int kMaxSequenceSize = 13;
189  int total_length = 0;
190  for (int loop = 0; loop < kLoops; loop++) {
191  int seq_length = loop % kMaxSequenceSize;
192  collector.StartSequence();
193  for (int j = 0; j < seq_length; j++) {
194  collector.Add(j);
195  }
196  Vector<int> sequence = collector.EndSequence();
197  for (int j = 0; j < seq_length; j++) {
198  CHECK_EQ(j, sequence[j]);
199  }
200  total_length += seq_length;
201  }
202  Vector<int> result = collector.ToVector();
203  CHECK_EQ(total_length, result.length());
204  int offset = 0;
205  for (int loop = 0; loop < kLoops; loop++) {
206  int seq_length = loop % kMaxSequenceSize;
207  for (int j = 0; j < seq_length; j++) {
208  CHECK_EQ(j, result[offset]);
209  offset++;
210  }
211  }
212  result.Dispose();
213 }
214 
215 
216 TEST(SequenceCollectorRegression) {
217  SequenceCollector<char> collector(16);
218  collector.StartSequence();
219  collector.Add('0');
220  collector.AddBlock(
221  i::Vector<const char>("12345678901234567890123456789012", 32));
222  i::Vector<char> seq = collector.EndSequence();
223  CHECK_EQ(0, strncmp("0123456789012345678901234567890123",
224  seq.start(), seq.length()));
225 }
#define CHECK_EQ(expected, value)
Definition: checks.h:219
void TestMemCopy(Vector< byte > src, Vector< byte > dst, int source_alignment, int destination_alignment, int length_alignment)
Definition: test-utils.cc:101
void Add(T value)
Definition: utils.h:566
#define CHECK(condition)
Definition: checks.h:56
static const int kMinComplexMemCopy
Definition: platform.h:342
uint8_t byte
Definition: globals.h:156
Vector< T > AddBlock(int size, T initial_value)
Definition: utils.h:579
T * start() const
Definition: utils.h:390
static void MemCopy(void *dest, const void *src, size_t size)
Definition: platform.h:339
int FastD2IChecked(double x)
Definition: conversions.h:58
int length() const
Definition: utils.h:384
Vector< T > ToVector()
Definition: utils.h:633
static Vector< T > New(int length)
Definition: utils.h:370
int StrLength(const char *string)
Definition: utils.h:234
static int SNPrintF(Vector< char > str, const char *format,...)
Vector< T > EndSequence()
Definition: utils.h:715
static double nan_value()
static void SetUp()
static bool Initialize()
Definition: api.cc:4269
int FastD2I(double x)
Definition: conversions.h:69