blob: b48dcb8b6ee6c2453c3b22d1a73abff29dcec031 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// 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
35using namespace v8::internal;
36
37
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000038TEST(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));
ager@chromium.orge2902be2009-06-08 12:21:35 +000050 // 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);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000056 CHECK_EQ(-2, static_cast<int>(static_cast<intptr_t>(-8) >> 2));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057}
58
59
60TEST(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!";
ager@chromium.orgc4c92722009-11-18 14:12:51 +000064 int length = StrLength(s);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000065 for (int i = 1; i < length * 2; i++) {
66 static const char kMarker = static_cast<char>(42);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000067 Vector<char> buffer = Vector<char>::New(i + 1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000068 buffer[i] = kMarker;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000069 int n = OS::SNPrintF(Vector<char>(buffer.start(), i), "%s", s);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000070 CHECK(n <= i);
71 CHECK(n == length || n == -1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000072 CHECK_EQ(0, strncmp(buffer.start(), s, i - 1));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000073 CHECK_EQ(kMarker, buffer[i]);
74 if (i <= length) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000075 CHECK_EQ(i - 1, StrLength(buffer.start()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000077 CHECK_EQ(length, StrLength(buffer.start()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000078 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000079 buffer.Dispose();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000080 }
81}
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +000082
83
84void 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 = kMinComplexMemCopy + length_alignment;
93 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
105TEST(MemCopy) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000106 V8::Initialize(NULL);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000107 const int N = kMinComplexMemCopy + 128;
108 Vector<byte> buffer1 = Vector<byte>::New(N);
109 Vector<byte> buffer2 = Vector<byte>::New(N);
110
111 for (int i = 0; i < N; i++) {
112 buffer1[i] = static_cast<byte>(i & 0x7F);
113 }
114
115 // Same alignment.
116 for (int i = 0; i < 32; i++) {
117 TestMemCopy(buffer1, buffer2, i, i, i * 2);
118 }
119
120 // Different alignment.
121 for (int i = 0; i < 32; i++) {
122 for (int j = 1; j < 32; j++) {
123 TestMemCopy(buffer1, buffer2, i, (i + j) & 0x1F , 0);
124 }
125 }
126
127 // Different lengths
128 for (int i = 0; i < 32; i++) {
129 TestMemCopy(buffer1, buffer2, 3, 7, i);
130 }
131
132 buffer2.Dispose();
133 buffer1.Dispose();
134}
ricow@chromium.org65fae842010-08-25 15:26:24 +0000135
136
137TEST(Collector) {
138 Collector<int> collector(8);
139 const int kLoops = 5;
140 const int kSequentialSize = 1000;
141 const int kBlockSize = 7;
142 for (int loop = 0; loop < kLoops; loop++) {
143 Vector<int> block = collector.AddBlock(7, 0xbadcafe);
144 for (int i = 0; i < kSequentialSize; i++) {
145 collector.Add(i);
146 }
147 for (int i = 0; i < kBlockSize - 1; i++) {
148 block[i] = i * 7;
149 }
150 }
151 Vector<int> result = collector.ToVector();
152 CHECK_EQ(kLoops * (kBlockSize + kSequentialSize), result.length());
153 for (int i = 0; i < kLoops; i++) {
154 int offset = i * (kSequentialSize + kBlockSize);
155 for (int j = 0; j < kBlockSize - 1; j++) {
156 CHECK_EQ(j * 7, result[offset + j]);
157 }
158 CHECK_EQ(0xbadcafe, result[offset + kBlockSize - 1]);
159 for (int j = 0; j < kSequentialSize; j++) {
160 CHECK_EQ(j, result[offset + kBlockSize + j]);
161 }
162 }
163 result.Dispose();
164}
165
166
167TEST(SequenceCollector) {
168 SequenceCollector<int> collector(8);
169 const int kLoops = 5000;
170 const int kMaxSequenceSize = 13;
171 int total_length = 0;
172 for (int loop = 0; loop < kLoops; loop++) {
173 int seq_length = loop % kMaxSequenceSize;
174 collector.StartSequence();
175 for (int j = 0; j < seq_length; j++) {
176 collector.Add(j);
177 }
178 Vector<int> sequence = collector.EndSequence();
179 for (int j = 0; j < seq_length; j++) {
180 CHECK_EQ(j, sequence[j]);
181 }
182 total_length += seq_length;
183 }
184 Vector<int> result = collector.ToVector();
185 CHECK_EQ(total_length, result.length());
186 int offset = 0;
187 for (int loop = 0; loop < kLoops; loop++) {
188 int seq_length = loop % kMaxSequenceSize;
189 for (int j = 0; j < seq_length; j++) {
190 CHECK_EQ(j, result[offset]);
191 offset++;
192 }
193 }
194 result.Dispose();
195}