The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / cctest / test-utils.cc @ 40c0f755

History | View | Annotate | Download (4.94 KB)

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

    
38
enum Mode {
39
  forward,
40
  backward_unsigned
41
};
42

    
43

    
44
static 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

    
60
static 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

    
81
static 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

    
97
static 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

    
113
void 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

    
131
TEST(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

    
143
TEST(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
}
156

    
157

    
158
TEST(SNPrintF) {
159
  // Make sure that strings that are truncated because of too small
160
  // buffers are zero-terminated anyway.
161
  const char* s = "the quick lazy .... oh forget it!";
162
  int length = strlen(s);
163
  for (int i = 1; i < length * 2; i++) {
164
    static const char kMarker = static_cast<char>(42);
165
    Vector<char> buffer = Vector<char>::New(i + 1);
166
    buffer[i] = kMarker;
167
    int n = OS::SNPrintF(Vector<char>(buffer.start(), i), "%s", s);
168
    CHECK(n <= i);
169
    CHECK(n == length || n == -1);
170
    CHECK_EQ(0, strncmp(buffer.start(), s, i - 1));
171
    CHECK_EQ(kMarker, buffer[i]);
172
    if (i <= length) {
173
      CHECK_EQ(i - 1, strlen(buffer.start()));
174
    } else {
175
      CHECK_EQ(length, strlen(buffer.start()));
176
    }
177
    buffer.Dispose();
178
  }
179
}