blob: 15a854b363c42e613cb09593f81d196525712060 [file] [log] [blame]
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001// Copyright 2011 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 "api.h"
31#include "debug.h"
32#include "execution.h"
33#include "factory.h"
34#include "macro-assembler.h"
35#include "objects.h"
36#include "global-handles.h"
37#include "cctest.h"
38
39using namespace v8::internal;
40
41TEST(ObjectHashTable) {
42 v8::HandleScope scope;
43 LocalContext context;
44 Handle<ObjectHashTable> table = FACTORY->NewObjectHashTable(23);
45 Handle<JSObject> a = FACTORY->NewJSArray(7);
46 Handle<JSObject> b = FACTORY->NewJSArray(11);
47 table = PutIntoObjectHashTable(table, a, b);
48 CHECK_EQ(table->NumberOfElements(), 1);
49 CHECK_EQ(table->Lookup(*a), *b);
50 CHECK_EQ(table->Lookup(*b), HEAP->undefined_value());
51
52 // Keys still have to be valid after objects were moved.
53 HEAP->CollectGarbage(NEW_SPACE);
54 CHECK_EQ(table->NumberOfElements(), 1);
55 CHECK_EQ(table->Lookup(*a), *b);
56 CHECK_EQ(table->Lookup(*b), HEAP->undefined_value());
57
58 // Keys that are overwritten should not change number of elements.
59 table = PutIntoObjectHashTable(table, a, FACTORY->NewJSArray(13));
60 CHECK_EQ(table->NumberOfElements(), 1);
61 CHECK_NE(table->Lookup(*a), *b);
62
63 // Keys mapped to undefined should be removed permanently.
64 table = PutIntoObjectHashTable(table, a, FACTORY->undefined_value());
65 CHECK_EQ(table->NumberOfElements(), 0);
66 CHECK_EQ(table->NumberOfDeletedElements(), 1);
67 CHECK_EQ(table->Lookup(*a), HEAP->undefined_value());
68
69 // Keys should map back to their respective values.
70 for (int i = 0; i < 100; i++) {
71 Handle<JSObject> key = FACTORY->NewJSArray(7);
72 Handle<JSObject> value = FACTORY->NewJSArray(11);
73 table = PutIntoObjectHashTable(table, key, value);
74 CHECK_EQ(table->NumberOfElements(), i + 1);
75 CHECK_NE(table->FindEntry(*key), ObjectHashTable::kNotFound);
76 CHECK_EQ(table->Lookup(*key), *value);
77 }
78
79 // Keys never added to the map should not be found.
80 for (int i = 0; i < 1000; i++) {
81 Handle<JSObject> o = FACTORY->NewJSArray(100);
82 CHECK_EQ(table->FindEntry(*o), ObjectHashTable::kNotFound);
83 CHECK_EQ(table->Lookup(*o), HEAP->undefined_value());
84 }
85}