blob: bcb185d24fbcc6344c43e22abacdd9a9a42ca871 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
Steve Blocka7e24c12009-10-30 11:49:00 +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));
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
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!";
Steve Blockd0582a62009-12-15 09:54:21 +000064 int length = StrLength(s);
Steve Blocka7e24c12009-10-30 11:49:00 +000065 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) {
Steve Blockd0582a62009-12-15 09:54:21 +000075 CHECK_EQ(i - 1, StrLength(buffer.start()));
Steve Blocka7e24c12009-10-30 11:49:00 +000076 } else {
Steve Blockd0582a62009-12-15 09:54:21 +000077 CHECK_EQ(length, StrLength(buffer.start()));
Steve Blocka7e24c12009-10-30 11:49:00 +000078 }
79 buffer.Dispose();
80 }
81}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010082
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) {
106 const int N = kMinComplexMemCopy + 128;
107 Vector<byte> buffer1 = Vector<byte>::New(N);
108 Vector<byte> buffer2 = Vector<byte>::New(N);
109
110 for (int i = 0; i < N; i++) {
111 buffer1[i] = static_cast<byte>(i & 0x7F);
112 }
113
114 // Same alignment.
115 for (int i = 0; i < 32; i++) {
116 TestMemCopy(buffer1, buffer2, i, i, i * 2);
117 }
118
119 // Different alignment.
120 for (int i = 0; i < 32; i++) {
121 for (int j = 1; j < 32; j++) {
122 TestMemCopy(buffer1, buffer2, i, (i + j) & 0x1F , 0);
123 }
124 }
125
126 // Different lengths
127 for (int i = 0; i < 32; i++) {
128 TestMemCopy(buffer1, buffer2, 3, 7, i);
129 }
130
131 buffer2.Dispose();
132 buffer1.Dispose();
133}