xref: /aosp_15_r20/external/swiftshader/tests/SystemUnitTests/ConfiguratorTests.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2022 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "System/Configurator.hpp"
16 
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include <cstdlib>
21 #include <sstream>
22 
23 using namespace sw;
24 
TEST(Configurator,IntegerOptionsAreParsedCorrectly)25 TEST(Configurator, IntegerOptionsAreParsedCorrectly)
26 {
27 	std::istringstream config{ R"(
28     [SectionA]
29     OptionA = 8
30     OptionB = 0xff
31     OptionC = -10
32     )" };
33 	Configurator configurator{ config };
34 
35 	EXPECT_EQ(configurator.getInteger("SectionA", "OptionA", 0), 8);
36 	EXPECT_EQ(configurator.getInteger("SectionA", "OptionB", 0), 255);
37 	EXPECT_EQ(configurator.getInteger("SectionA", "OptionC", 0), -10);
38 }
39 
TEST(Configurator,FloatOptionsAreParsedCorrectly)40 TEST(Configurator, FloatOptionsAreParsedCorrectly)
41 {
42 	std::istringstream config{ R"(
43     [SectionA]
44     OptionA = 1.25
45     OptionB = 3
46     OptionC = 1e2
47     OptionD = -1.5
48     )" };
49 	Configurator configurator{ config };
50 
51 	EXPECT_EQ(configurator.getFloat("SectionA", "OptionA", 0.0f), 1.25f);
52 	EXPECT_EQ(configurator.getFloat("SectionA", "OptionB", 0.0f), 3.0f);
53 	EXPECT_EQ(configurator.getFloat("SectionA", "OptionC", 0.0f), 100.0f);
54 	EXPECT_EQ(configurator.getFloat("SectionA", "OptionD", 0.0f), -1.5f);
55 }
56 
TEST(Configurator,BooleanOptionsAreParsedCorrectly)57 TEST(Configurator, BooleanOptionsAreParsedCorrectly)
58 {
59 	std::istringstream config{ R"(
60     [SectionA]
61     OptionA = true
62     OptionB = false
63     OptionC = 1
64     OptionD = 0
65     )" };
66 	Configurator configurator{ config };
67 
68 	EXPECT_EQ(configurator.getBoolean("SectionA", "OptionA", false), true);
69 	EXPECT_EQ(configurator.getBoolean("SectionA", "OptionB", true), false);
70 	EXPECT_EQ(configurator.getBoolean("SectionA", "OptionC", false), true);
71 	EXPECT_EQ(configurator.getBoolean("SectionA", "OptionD", true), false);
72 }
73 
TEST(Configurator,MultipleSectionsSameKeyAreDistinguished)74 TEST(Configurator, MultipleSectionsSameKeyAreDistinguished)
75 {
76 	std::istringstream config{ R"(
77     [SectionA]
78     OptionA = 1
79 
80     [SectionB]
81     OptionA = 2
82     )" };
83 	Configurator configurator{ config };
84 
85 	EXPECT_EQ(configurator.getInteger("SectionA", "OptionA", 0), 1);
86 	EXPECT_EQ(configurator.getInteger("SectionB", "OptionA", 0), 2);
87 }
88 
TEST(Configurator,SameKeyRepeatedHasLastValue)89 TEST(Configurator, SameKeyRepeatedHasLastValue)
90 {
91 	std::istringstream config{ R"(
92     [SectionA]
93     OptionA = 1
94     OptionA = 2
95     )" };
96 	Configurator configurator{ config };
97 
98 	EXPECT_EQ(configurator.getInteger("SectionA", "OptionA", 0), 2);
99 }
100 
TEST(Configurator,NonExistentKeyReturnsDefault)101 TEST(Configurator, NonExistentKeyReturnsDefault)
102 {
103 	std::istringstream config{ R"(
104     [SectionA]
105     OptionA = 1
106     )" };
107 	Configurator configurator{ config };
108 
109 	EXPECT_EQ(configurator.getInteger("SectionA", "NonExistentOption", 123), 123);
110 	EXPECT_EQ(configurator.getFloat("SectionA", "NonExistentOption", 1.5f), 1.5f);
111 	EXPECT_EQ(configurator.getBoolean("SectionA", "NonExistentOption", true), true);
112 }
113 
TEST(Configurator,SectionlessOptions)114 TEST(Configurator, SectionlessOptions)
115 {
116 	std::istringstream config{ R"(
117     OptionA = 8
118     OptionB = 1.5
119     )" };
120 	Configurator configurator{ config };
121 
122 	EXPECT_EQ(configurator.getInteger("", "OptionA", 0), 8);
123 	EXPECT_EQ(configurator.getFloat("", "OptionB", 0), 1.5f);
124 }