1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #include "glow/Support/Support.h" |
18 | #include "glow/Testing/StrCheck.h" |
19 | #include "gtest/gtest.h" |
20 | |
21 | #ifndef GLOW_DATA_PATH |
22 | #define GLOW_DATA_PATH |
23 | #endif |
24 | |
25 | using namespace glow; |
26 | using glow::StrCheck; |
27 | |
28 | TEST(Support, strFormat) { |
29 | // Check single-line formatted output. |
30 | std::string str1 = strFormat("%s %d %c" , "string1" , 123, 'x'); |
31 | EXPECT_TRUE(StrCheck(str1).sameln("string1" ).sameln("123" ).sameln("x" )); |
32 | |
33 | // Check multi-line formatted output. |
34 | std::string str2 = strFormat("%s\n%d\n%c\n" , "string2" , 456, 'y'); |
35 | EXPECT_TRUE(StrCheck(str2).check("string2" ).check("456" ).check("y" )); |
36 | // Output is not a single line. |
37 | EXPECT_FALSE(StrCheck(str2).sameln("string2" ).sameln("456" ).sameln("y" )); |
38 | } |
39 | |
40 | TEST(Support, legalizeName) { |
41 | // Check that a name can't be empty. |
42 | std::string str1 = legalizeName("" ); |
43 | EXPECT_TRUE(str1.compare("A" ) == 0); |
44 | |
45 | // Check that a name must start with some alphabetic character or underscore. |
46 | std::string str2 = legalizeName("1abc_/abc" ); |
47 | EXPECT_TRUE(str2.compare("A1abc__abc" ) == 0); |
48 | |
49 | // Check that a legal name won't be converted. |
50 | std::string str3 = legalizeName("abc_1aBc" ); |
51 | EXPECT_TRUE(str3.compare("abc_1aBc" ) == 0); |
52 | |
53 | // Check that a long name is truncated. |
54 | std::string str4 = |
55 | legalizeName("__________string_with_45_characters_v1_______" , 30); |
56 | EXPECT_EQ("_____trunc_1743199862606518811" , str4); |
57 | |
58 | // Check that a long name is truncated and gets different suffix. |
59 | std::string str5 = |
60 | legalizeName("__________string_with_45_characters_v2_______" , 30); |
61 | EXPECT_EQ("_____trunc_2836065357594492436" , str5); |
62 | |
63 | // Check that equal long string are truncated to the same string. |
64 | std::string str6 = |
65 | legalizeName("__________string_with_45_characters_v2_______" , 30); |
66 | EXPECT_EQ("_____trunc_2836065357594492436" , str6); |
67 | } |
68 | |
69 | /// Check the reading Device config from a yaml file. |
70 | TEST(Support, loadYamlFile) { |
71 | std::string yamlFilename(GLOW_DATA_PATH "tests/runtime_test/cpuConfigs.yaml" ); |
72 | std::vector<DeviceConfigHelper> lists; |
73 | lists = deserializeDeviceConfigFromYaml(yamlFilename); |
74 | EXPECT_EQ(lists.size(), 2); |
75 | // Check the loading items. |
76 | // The config file is: |
77 | //--- |
78 | //- name: Device1 |
79 | // backendName: CPU |
80 | // parameters: | |
81 | // "platformID":"1" |
82 | // "deviceID" : "0" |
83 | // - name: Device2 |
84 | // backendName: CPU |
85 | // parameters: | |
86 | // "platformID":"1" |
87 | //... |
88 | EXPECT_EQ(lists[0].backendName_, "CPU" ); |
89 | EXPECT_EQ(lists[0].name_, "Device1" ); |
90 | EXPECT_EQ(lists[0].parameters_.str, |
91 | "\"platformID\":\"1\"\n\"deviceID\" : \"0\"\n" ); |
92 | EXPECT_EQ(lists[1].backendName_, "CPU" ); |
93 | EXPECT_EQ(lists[1].name_, "Device2" ); |
94 | EXPECT_EQ(lists[1].parameters_.str, "\"platformID\":\"1\"\n" ); |
95 | } |
96 | |
97 | TEST(Support, loadStrStrMapYamlFile) { |
98 | std::string yamlFilename(GLOW_DATA_PATH |
99 | "tests/runtime_test/backendSpecificOpts.yaml" ); |
100 | auto map = deserializeStrStrMapFromYaml(yamlFilename); |
101 | EXPECT_EQ(map.size(), 2); |
102 | // Check the loading items. |
103 | // The config file is: |
104 | // --- |
105 | // backendOption1: 'foo' |
106 | // backendOption2: 'bar' |
107 | // ... |
108 | EXPECT_EQ(map["backendOption1" ], "foo" ); |
109 | EXPECT_EQ(map["backendOption2" ], "bar" ); |
110 | } |
111 | |
112 | TEST(Support, ScopeGuard) { |
113 | int val = 1; |
114 | { |
115 | ScopeGuard guard([&]() { val++; }); |
116 | EXPECT_EQ(val, 1); |
117 | } |
118 | EXPECT_EQ(val, 2); |
119 | } |
120 | |
121 | TEST(Support, ScopeGuardDismiss) { |
122 | int val = 1; |
123 | { |
124 | ScopeGuard guard([&]() { val++; }); |
125 | EXPECT_EQ(val, 1); |
126 | guard.dismiss(); |
127 | } |
128 | EXPECT_EQ(val, 1); |
129 | } |
130 | |
131 | TEST(Support, ScopeGuardRunAndDismiss) { |
132 | int val = 1; |
133 | { |
134 | ScopeGuard guard([&]() { val++; }); |
135 | EXPECT_EQ(val, 1); |
136 | guard.runAndDismiss(); |
137 | } |
138 | EXPECT_EQ(val, 2); |
139 | } |
140 | |
141 | TEST(Support, ScopeGuardRunAndDismissMulti) { |
142 | int val = 1; |
143 | { |
144 | ScopeGuard guard([&]() { val++; }); |
145 | EXPECT_EQ(val, 1); |
146 | guard.runAndDismiss(); |
147 | guard.runAndDismiss(); // Should be ignored as already dismissed. |
148 | } |
149 | EXPECT_EQ(val, 2); |
150 | } |
151 | |
152 | TEST(Support, ScopeGuardDismissAndRunAndDismiss) { |
153 | int val = 1; |
154 | { |
155 | ScopeGuard guard([&]() { val++; }); |
156 | EXPECT_EQ(val, 1); |
157 | guard.dismiss(); |
158 | guard.runAndDismiss(); // Should be ignored as already dismissed. |
159 | } |
160 | EXPECT_EQ(val, 1); |
161 | } |
162 | |
163 | TEST(Support, ArrayRefPrinting) { |
164 | // Check that when printing ArrayRef, |
165 | // the elements are omitted when reached limit and vice versa. |
166 | const std::vector<int> shortVec(4, 0); |
167 | const std::vector<int> longVec(5, 0); |
168 | const llvm::ArrayRef<int> shortArr(shortVec); |
169 | const llvm::ArrayRef<int> longArr(longVec); |
170 | std::ostringstream oss; |
171 | oss << shortArr << longArr; |
172 | EXPECT_TRUE(oss.str().compare("[0, 0, 0, 0][0, 0, 0, 0, ...]" ) == 0); |
173 | } |
174 | |