-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsymbol_factory_test.cpp
90 lines (69 loc) · 3.09 KB
/
symbol_factory_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
Copyright 2022 The UHDM Team.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <string>
#include <string_view>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "uhdm/SymbolFactory.h"
namespace UHDM {
using testing::ElementsAre;
namespace {
TEST(SymbolFactoryTest, SymbolFactoryAccess) {
SymbolFactory table;
const SymbolId foo_id = table.Make("foo");
EXPECT_NE(foo_id, SymbolFactory::getBadId());
const SymbolId bar_id = table.Make("bar");
EXPECT_NE(foo_id, bar_id);
// Attempting to register the same symbol will result in original ID.
EXPECT_EQ(table.Make("foo"), foo_id);
EXPECT_EQ(table.Make("bar"), bar_id);
// Retrieve symbol-ID by text string
EXPECT_EQ(table.GetId("foo"), foo_id);
EXPECT_EQ(table.GetId("bar"), bar_id);
EXPECT_EQ(table.GetId("baz"), SymbolFactory::getBadId()); // no-exist
// Make sure comparisons don't latch on the address of the string constants
// (as the linker will make all "foo" constants appear on the same address),
// but actually do the find by content
const std::string stringsource("foobar");
EXPECT_EQ(table.GetId(stringsource.substr(0, 3)), foo_id);
EXPECT_EQ(table.GetId(stringsource.substr(3, 3)), bar_id);
// Retrieve text symbol by ID
EXPECT_EQ(table.GetSymbol(foo_id), "foo");
EXPECT_EQ(table.GetSymbol(bar_id), "bar");
EXPECT_EQ(table.GetSymbol(SymbolId(42, "<whatever>")),
SymbolFactory::getBadSymbol()); // no-exist
EXPECT_EQ(table.GetSymbol(SymbolId(0, "")),
""); // no-exist, but zero however should return an empty string
// For now, symbols returned in getSymbols() always contain bad symbol as
// first element (though this is an implementation detail and might change).
EXPECT_THAT(table.getSymbols(),
ElementsAre(SymbolFactory::getBadSymbol(), "foo", "bar"));
}
TEST(SymbolFactoryTest, SymbolStringsAreStable) {
SymbolFactory table;
const SymbolId foo_id = table.Make("foo");
// Deliberately using .data() here so that API change to GetSymbol() returning
// std::string_view later will keep this test source-code compatible.
const char *before_data = table.GetSymbol(foo_id).data();
// We want to make sure that even after reallocing the underlying
// data structure, the symbol reference does not change. Let's enforce
// some reallocs by creating a bunch of symbols.
for (int32_t i = 0; i < 100000; ++i) {
table.Make("bar" + std::to_string(i));
}
const char *after_data = table.GetSymbol(foo_id).data();
EXPECT_EQ(before_data, after_data);
}
} // namespace
} // namespace UHDM