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