1 // Copyright 2006-2008 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 "platform.h"
33 #include "cctest.h"
34
35 using namespace v8::internal;
36
37
TEST(Utils1)38 TEST(Utils1) {
39 CHECK_EQ(-1000000, FastD2I(-1000000.0));
40 CHECK_EQ(-1, FastD2I(-1.0));
41 CHECK_EQ(0, FastD2I(0.0));
42 CHECK_EQ(1, FastD2I(1.0));
43 CHECK_EQ(1000000, FastD2I(1000000.0));
44
45 CHECK_EQ(-1000000, FastD2I(-1000000.123));
46 CHECK_EQ(-1, FastD2I(-1.234));
47 CHECK_EQ(0, FastD2I(0.345));
48 CHECK_EQ(1, FastD2I(1.234));
49 CHECK_EQ(1000000, FastD2I(1000000.123));
50 // Check that >> is implemented as arithmetic shift right.
51 // If this is not true, then ArithmeticShiftRight() must be changed,
52 // There are also documented right shifts in assembler.cc of
53 // int8_t and intptr_t signed integers.
54 CHECK_EQ(-2, -8 >> 2);
55 CHECK_EQ(-2, static_cast<int8_t>(-8) >> 2);
56 CHECK_EQ(-2, static_cast<int>(static_cast<intptr_t>(-8) >> 2));
57 }
58
59
TEST(SNPrintF)60 TEST(SNPrintF) {
61 // Make sure that strings that are truncated because of too small
62 // buffers are zero-terminated anyway.
63 const char* s = "the quick lazy .... oh forget it!";
64 int length = StrLength(s);
65 for (int i = 1; i < length * 2; i++) {
66 static const char kMarker = static_cast<char>(42);
67 Vector<char> buffer = Vector<char>::New(i + 1);
68 buffer[i] = kMarker;
69 int n = OS::SNPrintF(Vector<char>(buffer.start(), i), "%s", s);
70 CHECK(n <= i);
71 CHECK(n == length || n == -1);
72 CHECK_EQ(0, strncmp(buffer.start(), s, i - 1));
73 CHECK_EQ(kMarker, buffer[i]);
74 if (i <= length) {
75 CHECK_EQ(i - 1, StrLength(buffer.start()));
76 } else {
77 CHECK_EQ(length, StrLength(buffer.start()));
78 }
79 buffer.Dispose();
80 }
81 }
82
83
TestMemCopy(Vector<byte> src,Vector<byte> dst,int source_alignment,int destination_alignment,int length_alignment)84 void TestMemCopy(Vector<byte> src,
85 Vector<byte> dst,
86 int source_alignment,
87 int destination_alignment,
88 int length_alignment) {
89 memset(dst.start(), 0xFF, dst.length());
90 byte* to = dst.start() + 32 + destination_alignment;
91 byte* from = src.start() + source_alignment;
92 int length = OS::kMinComplexMemCopy + length_alignment;
93 OS::MemCopy(to, from, static_cast<size_t>(length));
94 printf("[%d,%d,%d]\n",
95 source_alignment, destination_alignment, length_alignment);
96 for (int i = 0; i < length; i++) {
97 CHECK_EQ(from[i], to[i]);
98 }
99 CHECK_EQ(0xFF, to[-1]);
100 CHECK_EQ(0xFF, to[length]);
101 }
102
103
104
TEST(MemCopy)105 TEST(MemCopy) {
106 v8::V8::Initialize();
107 OS::Setup();
108 const int N = OS::kMinComplexMemCopy + 128;
109 Vector<byte> buffer1 = Vector<byte>::New(N);
110 Vector<byte> buffer2 = Vector<byte>::New(N);
111
112 for (int i = 0; i < N; i++) {
113 buffer1[i] = static_cast<byte>(i & 0x7F);
114 }
115
116 // Same alignment.
117 for (int i = 0; i < 32; i++) {
118 TestMemCopy(buffer1, buffer2, i, i, i * 2);
119 }
120
121 // Different alignment.
122 for (int i = 0; i < 32; i++) {
123 for (int j = 1; j < 32; j++) {
124 TestMemCopy(buffer1, buffer2, i, (i + j) & 0x1F , 0);
125 }
126 }
127
128 // Different lengths
129 for (int i = 0; i < 32; i++) {
130 TestMemCopy(buffer1, buffer2, 3, 7, i);
131 }
132
133 buffer2.Dispose();
134 buffer1.Dispose();
135 }
136
137
TEST(Collector)138 TEST(Collector) {
139 Collector<int> collector(8);
140 const int kLoops = 5;
141 const int kSequentialSize = 1000;
142 const int kBlockSize = 7;
143 for (int loop = 0; loop < kLoops; loop++) {
144 Vector<int> block = collector.AddBlock(7, 0xbadcafe);
145 for (int i = 0; i < kSequentialSize; i++) {
146 collector.Add(i);
147 }
148 for (int i = 0; i < kBlockSize - 1; i++) {
149 block[i] = i * 7;
150 }
151 }
152 Vector<int> result = collector.ToVector();
153 CHECK_EQ(kLoops * (kBlockSize + kSequentialSize), result.length());
154 for (int i = 0; i < kLoops; i++) {
155 int offset = i * (kSequentialSize + kBlockSize);
156 for (int j = 0; j < kBlockSize - 1; j++) {
157 CHECK_EQ(j * 7, result[offset + j]);
158 }
159 CHECK_EQ(0xbadcafe, result[offset + kBlockSize - 1]);
160 for (int j = 0; j < kSequentialSize; j++) {
161 CHECK_EQ(j, result[offset + kBlockSize + j]);
162 }
163 }
164 result.Dispose();
165 }
166
167
TEST(SequenceCollector)168 TEST(SequenceCollector) {
169 SequenceCollector<int> collector(8);
170 const int kLoops = 5000;
171 const int kMaxSequenceSize = 13;
172 int total_length = 0;
173 for (int loop = 0; loop < kLoops; loop++) {
174 int seq_length = loop % kMaxSequenceSize;
175 collector.StartSequence();
176 for (int j = 0; j < seq_length; j++) {
177 collector.Add(j);
178 }
179 Vector<int> sequence = collector.EndSequence();
180 for (int j = 0; j < seq_length; j++) {
181 CHECK_EQ(j, sequence[j]);
182 }
183 total_length += seq_length;
184 }
185 Vector<int> result = collector.ToVector();
186 CHECK_EQ(total_length, result.length());
187 int offset = 0;
188 for (int loop = 0; loop < kLoops; loop++) {
189 int seq_length = loop % kMaxSequenceSize;
190 for (int j = 0; j < seq_length; j++) {
191 CHECK_EQ(j, result[offset]);
192 offset++;
193 }
194 }
195 result.Dispose();
196 }
197