Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include <cmath>
6 : #include <iostream>
7 : #include <limits>
8 :
9 : #include "src/objects-inl.h"
10 : #include "src/objects.h"
11 : #include "test/unittests/test-utils.h"
12 :
13 : #include "testing/gtest/include/gtest/gtest.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 13158 : TEST(Object, InstanceTypeListOrder) {
19 1 : int current = 0;
20 1 : int last = -1;
21 1 : InstanceType current_type = static_cast<InstanceType>(current);
22 2 : EXPECT_EQ(current_type, InstanceType::FIRST_TYPE);
23 2 : EXPECT_EQ(current_type, InstanceType::INTERNALIZED_STRING_TYPE);
24 : #define TEST_INSTANCE_TYPE(type) \
25 : current_type = InstanceType::type; \
26 : current = static_cast<int>(current_type); \
27 : if (current > static_cast<int>(LAST_NAME_TYPE)) { \
28 : EXPECT_EQ(last + 1, current); \
29 : } \
30 : EXPECT_LT(last, current) << " INSTANCE_TYPE_LIST is not ordered: " \
31 : << "last = " << static_cast<InstanceType>(last) \
32 : << " vs. current = " << current_type; \
33 : last = current;
34 :
35 404 : INSTANCE_TYPE_LIST(TEST_INSTANCE_TYPE)
36 : #undef TEST_INSTANCE_TYPE
37 1 : }
38 :
39 13158 : TEST(Object, StructListOrder) {
40 1 : int current = static_cast<int>(InstanceType::ACCESSOR_INFO_TYPE);
41 1 : int last = current - 1;
42 2 : ASSERT_LT(0, last);
43 : InstanceType current_type = static_cast<InstanceType>(current);
44 : #define TEST_STRUCT(type, class, name) \
45 : current_type = InstanceType::type##_TYPE; \
46 : current = static_cast<int>(current_type); \
47 : EXPECT_EQ(last + 1, current) \
48 : << " STRUCT_LIST is not ordered: " \
49 : << " last = " << static_cast<InstanceType>(last) \
50 : << " vs. current = " << current_type; \
51 : last = current;
52 :
53 44 : STRUCT_LIST(TEST_STRUCT)
54 : #undef TEST_STRUCT
55 : }
56 :
57 : typedef TestWithIsolate ObjectWithIsolate;
58 :
59 13160 : TEST_F(ObjectWithIsolate, DictionaryGrowth) {
60 : Handle<SeededNumberDictionary> dict =
61 1 : SeededNumberDictionary::New(isolate(), 1);
62 : Handle<Object> value = isolate()->factory()->null_value();
63 1 : PropertyDetails details = PropertyDetails::Empty();
64 :
65 : // This test documents the expected growth behavior of a dictionary getting
66 : // elements added to it one by one.
67 : STATIC_ASSERT(HashTableBase::kMinCapacity == 4);
68 : uint32_t i = 1;
69 : // 3 elements fit into the initial capacity.
70 4 : for (; i <= 3; i++) {
71 3 : dict = SeededNumberDictionary::Add(dict, i, value, details);
72 3 : CHECK_EQ(4, dict->Capacity());
73 : }
74 : // 4th element triggers growth.
75 : DCHECK_EQ(4, i);
76 2 : for (; i <= 5; i++) {
77 2 : dict = SeededNumberDictionary::Add(dict, i, value, details);
78 2 : CHECK_EQ(8, dict->Capacity());
79 : }
80 : // 6th element triggers growth.
81 : DCHECK_EQ(6, i);
82 6 : for (; i <= 11; i++) {
83 6 : dict = SeededNumberDictionary::Add(dict, i, value, details);
84 6 : CHECK_EQ(16, dict->Capacity());
85 : }
86 : // 12th element triggers growth.
87 : DCHECK_EQ(12, i);
88 10 : for (; i <= 21; i++) {
89 10 : dict = SeededNumberDictionary::Add(dict, i, value, details);
90 10 : CHECK_EQ(32, dict->Capacity());
91 : }
92 : // 22nd element triggers growth.
93 : DCHECK_EQ(22, i);
94 22 : for (; i <= 43; i++) {
95 22 : dict = SeededNumberDictionary::Add(dict, i, value, details);
96 22 : CHECK_EQ(64, dict->Capacity());
97 : }
98 : // 44th element triggers growth.
99 : DCHECK_EQ(44, i);
100 7 : for (; i <= 50; i++) {
101 7 : dict = SeededNumberDictionary::Add(dict, i, value, details);
102 7 : CHECK_EQ(128, dict->Capacity());
103 : }
104 :
105 : // If we grow by larger chunks, the next (sufficiently big) power of 2 is
106 : // chosen as the capacity.
107 1 : dict = SeededNumberDictionary::New(isolate(), 1);
108 1 : dict = SeededNumberDictionary::EnsureCapacity(dict, 65);
109 1 : CHECK_EQ(128, dict->Capacity());
110 :
111 1 : dict = SeededNumberDictionary::New(isolate(), 1);
112 1 : dict = SeededNumberDictionary::EnsureCapacity(dict, 30);
113 1 : CHECK_EQ(64, dict->Capacity());
114 1 : }
115 :
116 : } // namespace internal
117 7893 : } // namespace v8
|