| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include <stdlib.h> | 28 #include <stdlib.h> |
| 29 | 29 |
| 30 #include "v8.h" | 30 #include "v8.h" |
| 31 | 31 |
| 32 #include "platform.h" | 32 #include "platform.h" |
| 33 #include "cctest.h" | 33 #include "cctest.h" |
| 34 | 34 |
| 35 using namespace v8::internal; | 35 using namespace v8::internal; |
| 36 | 36 |
| 37 | 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) { | 38 TEST(Utils1) { |
| 144 CHECK_EQ(-1000000, FastD2I(-1000000.0)); | 39 CHECK_EQ(-1000000, FastD2I(-1000000.0)); |
| 145 CHECK_EQ(-1, FastD2I(-1.0)); | 40 CHECK_EQ(-1, FastD2I(-1.0)); |
| 146 CHECK_EQ(0, FastD2I(0.0)); | 41 CHECK_EQ(0, FastD2I(0.0)); |
| 147 CHECK_EQ(1, FastD2I(1.0)); | 42 CHECK_EQ(1, FastD2I(1.0)); |
| 148 CHECK_EQ(1000000, FastD2I(1000000.0)); | 43 CHECK_EQ(1000000, FastD2I(1000000.0)); |
| 149 | 44 |
| 150 CHECK_EQ(-1000000, FastD2I(-1000000.123)); | 45 CHECK_EQ(-1000000, FastD2I(-1000000.123)); |
| 151 CHECK_EQ(-1, FastD2I(-1.234)); | 46 CHECK_EQ(-1, FastD2I(-1.234)); |
| 152 CHECK_EQ(0, FastD2I(0.345)); | 47 CHECK_EQ(0, FastD2I(0.345)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 177 CHECK_EQ(0, strncmp(buffer.start(), s, i - 1)); | 72 CHECK_EQ(0, strncmp(buffer.start(), s, i - 1)); |
| 178 CHECK_EQ(kMarker, buffer[i]); | 73 CHECK_EQ(kMarker, buffer[i]); |
| 179 if (i <= length) { | 74 if (i <= length) { |
| 180 CHECK_EQ(i - 1, StrLength(buffer.start())); | 75 CHECK_EQ(i - 1, StrLength(buffer.start())); |
| 181 } else { | 76 } else { |
| 182 CHECK_EQ(length, StrLength(buffer.start())); | 77 CHECK_EQ(length, StrLength(buffer.start())); |
| 183 } | 78 } |
| 184 buffer.Dispose(); | 79 buffer.Dispose(); |
| 185 } | 80 } |
| 186 } | 81 } |
| OLD | NEW |