ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 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); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 56 | CHECK_EQ(-2, static_cast<int>(static_cast<intptr_t>(-8) >> 2)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
| 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!"; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 64 | int length = StrLength(s); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 65 | for (int i = 1; i < length * 2; i++) { |
| 66 | static const char kMarker = static_cast<char>(42); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 67 | Vector<char> buffer = Vector<char>::New(i + 1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 68 | buffer[i] = kMarker; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 69 | int n = OS::SNPrintF(Vector<char>(buffer.start(), i), "%s", s); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 70 | CHECK(n <= i); |
| 71 | CHECK(n == length || n == -1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 72 | CHECK_EQ(0, strncmp(buffer.start(), s, i - 1)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 73 | CHECK_EQ(kMarker, buffer[i]); |
| 74 | if (i <= length) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 75 | CHECK_EQ(i - 1, StrLength(buffer.start())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 76 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 77 | CHECK_EQ(length, StrLength(buffer.start())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 78 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 79 | buffer.Dispose(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 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 = 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 | |
| 105 | TEST(MemCopy) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 106 | V8::Initialize(NULL); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 107 | 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.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 135 | |
| 136 | |
| 137 | TEST(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 | |
| 167 | TEST(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 | } |