forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnum.cpp
155 lines (137 loc) · 6.67 KB
/
Enum.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//===- swift/unittests/runtime/Enum.cpp - Enum tests --------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Runtime/Metadata.h"
#include "swift/Runtime/Enum.h"
#include "gtest/gtest.h"
using namespace swift;
// Mock up a value witness table for Builtin.Int8 will 254 and 255 as extra
// inhabitants.
ExtraInhabitantsValueWitnessTable Int8WithExtraInhabitantValueWitness
= {
// ValueWitnessTable
ValueWitnessTable{
#define STEAL_INT8_WITNESS(witness) _TWVBi8_.witness,
FOR_ALL_FUNCTION_VALUE_WITNESSES(STEAL_INT8_WITNESS)
#undef STEAL_INT8_WITNESS
_TWVBi8_.size,
_TWVBi8_.flags.withExtraInhabitants(true),
_TWVBi8_.stride
},
// extraInhabitantFlags
ExtraInhabitantFlags().withNumExtraInhabitants(2),
// storeExtraInhabitant
[](OpaqueValue *dest, int index, const Metadata *self) {
*reinterpret_cast<uint8_t*>(dest) = 254 + index;
},
// getExtraInhabitantIndex
[](const OpaqueValue *src, const Metadata *self) -> int {
uint8_t byte = *reinterpret_cast<const uint8_t*>(src);
if (byte >= 254)
return byte - 254;
return -1;
}
};
FullMetadata<OpaqueMetadata> XI_TMBi8_ = {
{&Int8WithExtraInhabitantValueWitness},
{{MetadataKind::Opaque}}
};
const OpaqueValue *asOpaque(const void *v) {
return reinterpret_cast<const OpaqueValue*>(v);
}
OpaqueValue *asOpaque(void *v) {
return reinterpret_cast<OpaqueValue*>(v);
}
int test_getEnumCaseSinglePayload(std::initializer_list<uint8_t> repr,
const FullOpaqueMetadata &metadata,
unsigned numEmptyCases) {
return swift_getEnumCaseSinglePayload(asOpaque(repr.begin()),
&metadata.base, numEmptyCases);
}
TEST(EnumTest, getEnumCaseSinglePayload) {
// Test with no XI.
ASSERT_EQ(-1, test_getEnumCaseSinglePayload({0, 0}, _TMBi8_, 512));
ASSERT_EQ(-1, test_getEnumCaseSinglePayload({255, 0}, _TMBi8_, 512));
ASSERT_EQ(0, test_getEnumCaseSinglePayload({0, 1}, _TMBi8_, 512));
ASSERT_EQ(255, test_getEnumCaseSinglePayload({255, 1}, _TMBi8_, 512));
ASSERT_EQ(511, test_getEnumCaseSinglePayload({255, 2}, _TMBi8_, 512));
ASSERT_EQ(-1, test_getEnumCaseSinglePayload({0, 0, 0}, _TMBi8_,
128*1024));
ASSERT_EQ(-1, test_getEnumCaseSinglePayload({255, 0, 0}, _TMBi8_,
128*1024));
ASSERT_EQ(65535 - 255,
test_getEnumCaseSinglePayload({0, 0, 1}, _TMBi8_,
128*1024));
// Test with XI.
ASSERT_EQ(-1, test_getEnumCaseSinglePayload({0}, XI_TMBi8_, 2));
ASSERT_EQ(-1, test_getEnumCaseSinglePayload({253}, XI_TMBi8_, 2));
ASSERT_EQ(0, test_getEnumCaseSinglePayload({254}, XI_TMBi8_, 2));
ASSERT_EQ(1, test_getEnumCaseSinglePayload({255}, XI_TMBi8_, 2));
ASSERT_EQ(-1, test_getEnumCaseSinglePayload({0, 0}, XI_TMBi8_, 4));
ASSERT_EQ(-1, test_getEnumCaseSinglePayload({253, 0}, XI_TMBi8_, 4));
ASSERT_EQ(0, test_getEnumCaseSinglePayload({254, 0}, XI_TMBi8_, 4));
ASSERT_EQ(1, test_getEnumCaseSinglePayload({255, 0}, XI_TMBi8_, 4));
ASSERT_EQ(2, test_getEnumCaseSinglePayload({0, 1}, XI_TMBi8_, 4));
ASSERT_EQ(3, test_getEnumCaseSinglePayload({1, 1}, XI_TMBi8_, 4));
}
bool test_storeEnumTagSinglePayload(std::initializer_list<uint8_t> after,
std::initializer_list<uint8_t> before,
const FullOpaqueMetadata &metadata,
unsigned whichCase,
unsigned numEmptyCases) {
assert(after.size() == before.size());
std::vector<uint8_t> buf;
buf.resize(before.size());
memcpy(buf.data(), before.begin(), before.size());
swift_storeEnumTagSinglePayload(asOpaque(buf.data()),
&metadata.base,
whichCase,
numEmptyCases);
return memcmp(buf.data(), after.begin(), after.size()) == 0;
}
TEST(EnumTest, storeEnumTagSinglePayload) {
// Test with no XI.
ASSERT_TRUE(test_storeEnumTagSinglePayload({219, 0}, {219, 123},
_TMBi8_, -1, 512));
ASSERT_TRUE(test_storeEnumTagSinglePayload({0, 1}, {219, 123},
_TMBi8_, 0, 512));
ASSERT_TRUE(test_storeEnumTagSinglePayload({255, 1}, {219, 123},
_TMBi8_, 255, 512));
ASSERT_TRUE(test_storeEnumTagSinglePayload({255, 2}, {219, 123},
_TMBi8_, 511, 512));
ASSERT_TRUE(test_storeEnumTagSinglePayload({219, 0, 0}, {219, 123, 77},
_TMBi8_, -1, 128*1024));
ASSERT_TRUE(test_storeEnumTagSinglePayload({0, 1, 0}, {219, 123, 77},
_TMBi8_, 0, 128*1024));
ASSERT_TRUE(test_storeEnumTagSinglePayload({255, 1, 0}, {219, 123, 77},
_TMBi8_, 255, 128*1024));
ASSERT_TRUE(test_storeEnumTagSinglePayload({0, 2, 0}, {219, 123, 77},
_TMBi8_, 256, 128*1024));
ASSERT_TRUE(test_storeEnumTagSinglePayload({255, 0, 2}, {219, 123, 77},
_TMBi8_, 128*1024 - 1, 128*1024));
// Test with XI.
ASSERT_TRUE(test_storeEnumTagSinglePayload({219}, {219},
XI_TMBi8_, -1, 2));
ASSERT_TRUE(test_storeEnumTagSinglePayload({254}, {219},
XI_TMBi8_, 0, 2));
ASSERT_TRUE(test_storeEnumTagSinglePayload({255}, {219},
XI_TMBi8_, 1, 2));
ASSERT_TRUE(test_storeEnumTagSinglePayload({219, 0}, {219, 123},
XI_TMBi8_, -1, 4));
ASSERT_TRUE(test_storeEnumTagSinglePayload({254, 0}, {219, 123},
XI_TMBi8_, 0, 4));
ASSERT_TRUE(test_storeEnumTagSinglePayload({255, 0}, {219, 123},
XI_TMBi8_, 1, 4));
ASSERT_TRUE(test_storeEnumTagSinglePayload({0, 1}, {219, 123},
XI_TMBi8_, 2, 4));
ASSERT_TRUE(test_storeEnumTagSinglePayload({1, 1}, {219, 123},
XI_TMBi8_, 3, 4));
}