test-utils.cc revision a7e24c173cf37484693b9abb38e494fa7bd7baeb
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
35using namespace v8::internal;
36
37
38enum Mode {
39  forward,
40  backward_unsigned
41};
42
43
44static v8::internal::byte* Write(v8::internal::byte* p, Mode m, int x) {
45  v8::internal::byte* q = NULL;
46  switch (m) {
47    case forward:
48      q = EncodeInt(p, x);
49      CHECK(q <= p + sizeof(x) + 1);
50      break;
51    case backward_unsigned:
52      q = EncodeUnsignedIntBackward(p, x);
53      CHECK(q >= p - sizeof(x) - 1);
54      break;
55  }
56  return q;
57}
58
59
60static v8::internal::byte* Read(v8::internal::byte* p, Mode m, int x) {
61  v8::internal::byte* q = NULL;
62  int y;
63  switch (m) {
64    case forward:
65      q = DecodeInt(p, &y);
66      CHECK(q <= p + sizeof(y) + 1);
67      break;
68    case backward_unsigned: {
69      unsigned int uy;
70      q = DecodeUnsignedIntBackward(p, &uy);
71      y = uy;
72      CHECK(q >= p - sizeof(uy) - 1);
73      break;
74    }
75  }
76  CHECK(y == x);
77  return q;
78}
79
80
81static v8::internal::byte* WriteMany(v8::internal::byte* p, Mode m, int x) {
82  p = Write(p, m, x - 7);
83  p = Write(p, m, x - 1);
84  p = Write(p, m, x);
85  p = Write(p, m, x + 1);
86  p = Write(p, m, x + 2);
87  p = Write(p, m, -x - 5);
88  p = Write(p, m, -x - 1);
89  p = Write(p, m, -x);
90  p = Write(p, m, -x + 1);
91  p = Write(p, m, -x + 3);
92
93  return p;
94}
95
96
97static v8::internal::byte* ReadMany(v8::internal::byte* p, Mode m, int x) {
98  p = Read(p, m, x - 7);
99  p = Read(p, m, x - 1);
100  p = Read(p, m, x);
101  p = Read(p, m, x + 1);
102  p = Read(p, m, x + 2);
103  p = Read(p, m, -x - 5);
104  p = Read(p, m, -x - 1);
105  p = Read(p, m, -x);
106  p = Read(p, m, -x + 1);
107  p = Read(p, m, -x + 3);
108
109  return p;
110}
111
112
113void ProcessValues(int* values, int n, Mode m) {
114  v8::internal::byte buf[4 * KB];  // make this big enough
115  v8::internal::byte* p0 = (m == forward ? buf : buf + ARRAY_SIZE(buf));
116
117  v8::internal::byte* p = p0;
118  for (int i = 0; i < n; i++) {
119    p = WriteMany(p, m, values[i]);
120  }
121
122  v8::internal::byte* q = p0;
123  for (int i = 0; i < n; i++) {
124    q = ReadMany(q, m, values[i]);
125  }
126
127  CHECK(p == q);
128}
129
130
131TEST(Utils0) {
132  int values[] = {
133    0, 1, 10, 16, 32, 64, 128, 256, 512, 1024, 1234, 5731,
134    10000, 100000, 1000000, 10000000, 100000000, 1000000000
135  };
136  const int n = ARRAY_SIZE(values);
137
138  ProcessValues(values, n, forward);
139  ProcessValues(values, n, backward_unsigned);
140}
141
142
143TEST(Utils1) {
144  CHECK_EQ(-1000000, FastD2I(-1000000.0));
145  CHECK_EQ(-1, FastD2I(-1.0));
146  CHECK_EQ(0, FastD2I(0.0));
147  CHECK_EQ(1, FastD2I(1.0));
148  CHECK_EQ(1000000, FastD2I(1000000.0));
149
150  CHECK_EQ(-1000000, FastD2I(-1000000.123));
151  CHECK_EQ(-1, FastD2I(-1.234));
152  CHECK_EQ(0, FastD2I(0.345));
153  CHECK_EQ(1, FastD2I(1.234));
154  CHECK_EQ(1000000, FastD2I(1000000.123));
155  // Check that >> is implemented as arithmetic shift right.
156  // If this is not true, then ArithmeticShiftRight() must be changed,
157  // There are also documented right shifts in assembler.cc of
158  // int8_t and intptr_t signed integers.
159  CHECK_EQ(-2, -8 >> 2);
160  CHECK_EQ(-2, static_cast<int8_t>(-8) >> 2);
161  CHECK_EQ(-2, static_cast<int>(static_cast<intptr_t>(-8) >> 2));
162}
163
164
165TEST(SNPrintF) {
166  // Make sure that strings that are truncated because of too small
167  // buffers are zero-terminated anyway.
168  const char* s = "the quick lazy .... oh forget it!";
169  int length = strlen(s);
170  for (int i = 1; i < length * 2; i++) {
171    static const char kMarker = static_cast<char>(42);
172    Vector<char> buffer = Vector<char>::New(i + 1);
173    buffer[i] = kMarker;
174    int n = OS::SNPrintF(Vector<char>(buffer.start(), i), "%s", s);
175    CHECK(n <= i);
176    CHECK(n == length || n == -1);
177    CHECK_EQ(0, strncmp(buffer.start(), s, i - 1));
178    CHECK_EQ(kMarker, buffer[i]);
179    if (i <= length) {
180      CHECK_EQ(i - 1, strlen(buffer.start()));
181    } else {
182      CHECK_EQ(length, strlen(buffer.start()));
183    }
184    buffer.Dispose();
185  }
186}
187