OLD | NEW |
| (Empty) |
1 // Copyright 2012 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 "v8.h" | |
29 | |
30 #include "cctest.h" | |
31 #include "compiler.h" | |
32 #include "execution.h" | |
33 #include "isolate.h" | |
34 | |
35 | |
36 using namespace v8::internal; | |
37 | |
38 | |
39 void SetSeeds(Handle<ByteArray> seeds, uint32_t state0, uint32_t state1) { | |
40 for (int i = 0; i < 4; i++) { | |
41 seeds->set(i, static_cast<byte>(state0 >> (i * kBitsPerByte))); | |
42 seeds->set(i + 4, static_cast<byte>(state1 >> (i * kBitsPerByte))); | |
43 } | |
44 } | |
45 | |
46 | |
47 void TestSeeds(Handle<JSFunction> fun, | |
48 Handle<Context> context, | |
49 uint32_t state0, | |
50 uint32_t state1) { | |
51 bool has_pending_exception; | |
52 Handle<JSObject> global(context->global_object()); | |
53 Handle<ByteArray> seeds(context->random_seed()); | |
54 | |
55 SetSeeds(seeds, state0, state1); | |
56 Handle<Object> value = Execution::Call( | |
57 context->GetIsolate(), fun, global, 0, NULL, &has_pending_exception); | |
58 CHECK(value->IsHeapNumber()); | |
59 CHECK(fun->IsOptimized()); | |
60 double crankshaft_value = HeapNumber::cast(*value)->value(); | |
61 | |
62 SetSeeds(seeds, state0, state1); | |
63 V8::FillHeapNumberWithRandom(*value, *context); | |
64 double runtime_value = HeapNumber::cast(*value)->value(); | |
65 CHECK_EQ(runtime_value, crankshaft_value); | |
66 } | |
67 | |
68 | |
69 TEST(CrankshaftRandom) { | |
70 v8::V8::Initialize(); | |
71 // Skip test if crankshaft is disabled. | |
72 if (!CcTest::i_isolate()->use_crankshaft()) return; | |
73 v8::Isolate* v8_isolate = CcTest::isolate(); | |
74 v8::HandleScope scope(v8_isolate); | |
75 v8::Context::Scope context_scope(v8::Context::New(v8_isolate)); | |
76 | |
77 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); | |
78 Handle<Context> context(isolate->context()); | |
79 Handle<JSObject> global(context->global_object()); | |
80 Handle<ByteArray> seeds(context->random_seed()); | |
81 bool has_pending_exception; | |
82 | |
83 CompileRun("function f() { return Math.random(); }"); | |
84 | |
85 Object* string = CcTest::i_isolate()->factory()->InternalizeOneByteString( | |
86 STATIC_ASCII_VECTOR("f"))->ToObjectChecked(); | |
87 MaybeObject* fun_object = | |
88 context->global_object()->GetProperty(String::cast(string)); | |
89 Handle<JSFunction> fun(JSFunction::cast(fun_object->ToObjectChecked())); | |
90 | |
91 // Optimize function. | |
92 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | |
93 Execution::Call(isolate, fun, global, 0, NULL, &has_pending_exception); | |
94 if (!fun->IsOptimized()) fun->MarkForLazyRecompilation(); | |
95 | |
96 // Test with some random values. | |
97 TestSeeds(fun, context, 0xC0C0AFFE, 0x31415926); | |
98 TestSeeds(fun, context, 0x01020304, 0xFFFFFFFF); | |
99 TestSeeds(fun, context, 0x00000001, 0x00000001); | |
100 } | |
OLD | NEW |