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/base/platform/platform.h"
35 #include "test/cctest/cctest.h"
36
37 using namespace v8::internal;
38
39
TEST(Utils1)40 TEST(Utils1) {
41 CHECK_EQ(-1000000, FastD2I(-1000000.0));
42 CHECK_EQ(-1, FastD2I(-1.0));
43 CHECK_EQ(0, FastD2I(0.0));
44 CHECK_EQ(1, FastD2I(1.0));
45 CHECK_EQ(1000000, FastD2I(1000000.0));
46
47 CHECK_EQ(-1000000, FastD2I(-1000000.123));
48 CHECK_EQ(-1, FastD2I(-1.234));
49 CHECK_EQ(0, FastD2I(0.345));
50 CHECK_EQ(1, FastD2I(1.234));
51 CHECK_EQ(1000000, FastD2I(1000000.123));
52 // Check that >> is implemented as arithmetic shift right.
53 // If this is not true, then ArithmeticShiftRight() must be changed,
54 // There are also documented right shifts in assembler.cc of
55 // int8_t and intptr_t signed integers.
56 CHECK_EQ(-2, -8 >> 2);
57 CHECK_EQ(-2, static_cast<int8_t>(-8) >> 2);
58 CHECK_EQ(-2, static_cast<int>(static_cast<intptr_t>(-8) >> 2));
59
60 CHECK_EQ(-1000000, FastD2IChecked(-1000000.0));
61 CHECK_EQ(-1, FastD2IChecked(-1.0));
62 CHECK_EQ(0, FastD2IChecked(0.0));
63 CHECK_EQ(1, FastD2IChecked(1.0));
64 CHECK_EQ(1000000, FastD2IChecked(1000000.0));
65
66 CHECK_EQ(-1000000, FastD2IChecked(-1000000.123));
67 CHECK_EQ(-1, FastD2IChecked(-1.234));
68 CHECK_EQ(0, FastD2IChecked(0.345));
69 CHECK_EQ(1, FastD2IChecked(1.234));
70 CHECK_EQ(1000000, FastD2IChecked(1000000.123));
71
72 CHECK_EQ(INT_MAX, FastD2IChecked(1.0e100));
73 CHECK_EQ(INT_MIN, FastD2IChecked(-1.0e100));
74 CHECK_EQ(INT_MIN, FastD2IChecked(std::numeric_limits<double>::quiet_NaN()));
75 }
76
77
TEST(BitSetComputer)78 TEST(BitSetComputer) {
79 typedef BitSetComputer<bool, 1, kSmiValueSize, uint32_t> BoolComputer;
80 CHECK_EQ(0, BoolComputer::word_count(0));
81 CHECK_EQ(1, BoolComputer::word_count(8));
82 CHECK_EQ(2, BoolComputer::word_count(50));
83 CHECK_EQ(0, BoolComputer::index(0, 8));
84 CHECK_EQ(100, BoolComputer::index(100, 8));
85 CHECK_EQ(1, BoolComputer::index(0, 40));
86 uint32_t data = 0;
87 data = BoolComputer::encode(data, 1, true);
88 data = BoolComputer::encode(data, 4, true);
89 CHECK_EQ(true, BoolComputer::decode(data, 1));
90 CHECK_EQ(true, BoolComputer::decode(data, 4));
91 CHECK_EQ(false, BoolComputer::decode(data, 0));
92 CHECK_EQ(false, BoolComputer::decode(data, 2));
93 CHECK_EQ(false, BoolComputer::decode(data, 3));
94
95 // Lets store 2 bits per item with 3000 items and verify the values are
96 // correct.
97 typedef BitSetComputer<unsigned char, 2, 8, unsigned char> TwoBits;
98 const int words = 750;
99 CHECK_EQ(words, TwoBits::word_count(3000));
100 const int offset = 10;
101 Vector<unsigned char> buffer = Vector<unsigned char>::New(offset + words);
102 memset(buffer.start(), 0, sizeof(unsigned char) * buffer.length());
103 for (int i = 0; i < words; i++) {
104 const int index = TwoBits::index(offset, i);
105 unsigned char data = buffer[index];
106 data = TwoBits::encode(data, i, i % 4);
107 buffer[index] = data;
108 }
109
110 for (int i = 0; i < words; i++) {
111 const int index = TwoBits::index(offset, i);
112 unsigned char data = buffer[index];
113 CHECK_EQ(i % 4, TwoBits::decode(data, i));
114 }
115 buffer.Dispose();
116 }
117
118
TEST(SNPrintF)119 TEST(SNPrintF) {
120 // Make sure that strings that are truncated because of too small
121 // buffers are zero-terminated anyway.
122 const char* s = "the quick lazy .... oh forget it!";
123 int length = StrLength(s);
124 for (int i = 1; i < length * 2; i++) {
125 static const char kMarker = static_cast<char>(42);
126 Vector<char> buffer = Vector<char>::New(i + 1);
127 buffer[i] = kMarker;
128 int n = SNPrintF(Vector<char>(buffer.start(), i), "%s", s);
129 CHECK(n <= i);
130 CHECK(n == length || n == -1);
131 CHECK_EQ(0, strncmp(buffer.start(), s, i - 1));
132 CHECK_EQ(kMarker, buffer[i]);
133 if (i <= length) {
134 CHECK_EQ(i - 1, StrLength(buffer.start()));
135 } else {
136 CHECK_EQ(length, StrLength(buffer.start()));
137 }
138 buffer.Dispose();
139 }
140 }
141
142
143 static const int kAreaSize = 512;
144
145
TestMemMove(byte * area1,byte * area2,int src_offset,int dest_offset,int length)146 void TestMemMove(byte* area1,
147 byte* area2,
148 int src_offset,
149 int dest_offset,
150 int length) {
151 for (int i = 0; i < kAreaSize; i++) {
152 area1[i] = i & 0xFF;
153 area2[i] = i & 0xFF;
154 }
155 MemMove(area1 + dest_offset, area1 + src_offset, length);
156 memmove(area2 + dest_offset, area2 + src_offset, length);
157 if (memcmp(area1, area2, kAreaSize) != 0) {
158 printf("MemMove(): src_offset: %d, dest_offset: %d, length: %d\n",
159 src_offset, dest_offset, length);
160 for (int i = 0; i < kAreaSize; i++) {
161 if (area1[i] == area2[i]) continue;
162 printf("diff at offset %d (%p): is %d, should be %d\n", i,
163 reinterpret_cast<void*>(area1 + i), area1[i], area2[i]);
164 }
165 CHECK(false);
166 }
167 }
168
169
TEST(MemMove)170 TEST(MemMove) {
171 v8::V8::Initialize();
172 byte* area1 = new byte[kAreaSize];
173 byte* area2 = new byte[kAreaSize];
174
175 static const int kMinOffset = 32;
176 static const int kMaxOffset = 64;
177 static const int kMaxLength = 128;
178 STATIC_ASSERT(kMaxOffset + kMaxLength < kAreaSize);
179
180 for (int src_offset = kMinOffset; src_offset <= kMaxOffset; src_offset++) {
181 for (int dst_offset = kMinOffset; dst_offset <= kMaxOffset; dst_offset++) {
182 for (int length = 0; length <= kMaxLength; length++) {
183 TestMemMove(area1, area2, src_offset, dst_offset, length);
184 }
185 }
186 }
187 delete[] area1;
188 delete[] area2;
189 }
190
191
TEST(Collector)192 TEST(Collector) {
193 Collector<int> collector(8);
194 const int kLoops = 5;
195 const int kSequentialSize = 1000;
196 const int kBlockSize = 7;
197 for (int loop = 0; loop < kLoops; loop++) {
198 Vector<int> block = collector.AddBlock(7, 0xbadcafe);
199 for (int i = 0; i < kSequentialSize; i++) {
200 collector.Add(i);
201 }
202 for (int i = 0; i < kBlockSize - 1; i++) {
203 block[i] = i * 7;
204 }
205 }
206 Vector<int> result = collector.ToVector();
207 CHECK_EQ(kLoops * (kBlockSize + kSequentialSize), result.length());
208 for (int i = 0; i < kLoops; i++) {
209 int offset = i * (kSequentialSize + kBlockSize);
210 for (int j = 0; j < kBlockSize - 1; j++) {
211 CHECK_EQ(j * 7, result[offset + j]);
212 }
213 CHECK_EQ(0xbadcafe, result[offset + kBlockSize - 1]);
214 for (int j = 0; j < kSequentialSize; j++) {
215 CHECK_EQ(j, result[offset + kBlockSize + j]);
216 }
217 }
218 result.Dispose();
219 }
220
221
TEST(SequenceCollector)222 TEST(SequenceCollector) {
223 SequenceCollector<int> collector(8);
224 const int kLoops = 5000;
225 const int kMaxSequenceSize = 13;
226 int total_length = 0;
227 for (int loop = 0; loop < kLoops; loop++) {
228 int seq_length = loop % kMaxSequenceSize;
229 collector.StartSequence();
230 for (int j = 0; j < seq_length; j++) {
231 collector.Add(j);
232 }
233 Vector<int> sequence = collector.EndSequence();
234 for (int j = 0; j < seq_length; j++) {
235 CHECK_EQ(j, sequence[j]);
236 }
237 total_length += seq_length;
238 }
239 Vector<int> result = collector.ToVector();
240 CHECK_EQ(total_length, result.length());
241 int offset = 0;
242 for (int loop = 0; loop < kLoops; loop++) {
243 int seq_length = loop % kMaxSequenceSize;
244 for (int j = 0; j < seq_length; j++) {
245 CHECK_EQ(j, result[offset]);
246 offset++;
247 }
248 }
249 result.Dispose();
250 }
251
252
TEST(SequenceCollectorRegression)253 TEST(SequenceCollectorRegression) {
254 SequenceCollector<char> collector(16);
255 collector.StartSequence();
256 collector.Add('0');
257 collector.AddBlock(
258 i::Vector<const char>("12345678901234567890123456789012", 32));
259 i::Vector<char> seq = collector.EndSequence();
260 CHECK_EQ(0, strncmp("0123456789012345678901234567890123",
261 seq.start(), seq.length()));
262 }
263
264
TEST(CPlusPlus11Features)265 TEST(CPlusPlus11Features) {
266 struct S {
267 bool x;
268 struct T {
269 double y;
270 int z[3];
271 } t;
272 };
273 S s{true, {3.1415, {1, 2, 3}}};
274 CHECK_EQ(2, s.t.z[1]);
275
276 // TODO(svenpanne) Remove the old-skool code when we ship the new C++ headers.
277 #if 0
278 std::vector<int> vec{11, 22, 33, 44};
279 #else
280 std::vector<int> vec;
281 vec.push_back(11);
282 vec.push_back(22);
283 vec.push_back(33);
284 vec.push_back(44);
285 #endif
286 vec.push_back(55);
287 vec.push_back(66);
288 for (auto& i : vec) {
289 ++i;
290 }
291 int j = 12;
292 for (auto i : vec) {
293 CHECK_EQ(j, i);
294 j += 11;
295 }
296 }
297