File: ts-numeric.cpp

package info (click to toggle)
libsimpleini 4.25%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 724 kB
  • sloc: cpp: 4,599; makefile: 75; sh: 3
file content (316 lines) | stat: -rw-r--r-- 8,181 bytes parent folder | download
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "../SimpleIni.h"
#include "gtest/gtest.h"
#include <limits>
#include <climits>
#include <cmath>

class TestNumeric : public ::testing::Test {
protected:
	void SetUp() override;
protected:
	CSimpleIniA ini;
};

void TestNumeric::SetUp() {
	ini.SetUnicode();
}

// Test GetLongValue with valid integers
TEST_F(TestNumeric, TestGetLongValuePositive) {
	std::string input =
		"[numbers]\n"
		"positive = 42\n"
		"zero = 0\n"
		"negative = -123\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	long result = ini.GetLongValue("numbers", "positive", 0);
	ASSERT_EQ(result, 42);

	result = ini.GetLongValue("numbers", "zero", -1);
	ASSERT_EQ(result, 0);

	result = ini.GetLongValue("numbers", "negative", 0);
	ASSERT_EQ(result, -123);
}

// Test GetLongValue with hex values
TEST_F(TestNumeric, TestGetLongValueHex) {
	std::string input =
		"[numbers]\n"
		"hex1 = 0xFF\n"
		"hex2 = 0x10\n"
		"hex3 = 0x12345678\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	long result = ini.GetLongValue("numbers", "hex1", 0);
	ASSERT_EQ(result, 0xFF);

	result = ini.GetLongValue("numbers", "hex2", 0);
	ASSERT_EQ(result, 0x10);

	result = ini.GetLongValue("numbers", "hex3", 0);
	ASSERT_EQ(result, 0x12345678);
}

// Test GetLongValue with invalid values (should return default)
TEST_F(TestNumeric, TestGetLongValueInvalid) {
	std::string input =
		"[numbers]\n"
		"text = hello\n"
		"empty = \n"
		"partial = 123abc\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	long result = ini.GetLongValue("numbers", "text", 999);
	ASSERT_EQ(result, 999);

	// Empty string returns default (SimpleIni behavior, not raw strtol)
	result = ini.GetLongValue("numbers", "empty", 999);
	ASSERT_EQ(result, 999);

	// "123abc" returns default - SimpleIni validates the full string
	result = ini.GetLongValue("numbers", "partial", 999);
	ASSERT_EQ(result, 999);
}

// Test GetLongValue with non-existent key
TEST_F(TestNumeric, TestGetLongValueMissing) {
	std::string input = "[numbers]\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	long result = ini.GetLongValue("numbers", "missing", 777);
	ASSERT_EQ(result, 777);

	result = ini.GetLongValue("missing_section", "key", 888);
	ASSERT_EQ(result, 888);
}

// Test SetLongValue
TEST_F(TestNumeric, TestSetLongValue) {
	SI_Error rc = ini.SetLongValue("numbers", "value1", 12345);
	ASSERT_EQ(rc, SI_INSERTED);

	long result = ini.GetLongValue("numbers", "value1", 0);
	ASSERT_EQ(result, 12345);

	// Update existing value
	rc = ini.SetLongValue("numbers", "value1", 67890);
	ASSERT_EQ(rc, SI_UPDATED);

	result = ini.GetLongValue("numbers", "value1", 0);
	ASSERT_EQ(result, 67890);
}

// Test SetLongValue with hex format
TEST_F(TestNumeric, TestSetLongValueHex) {
	SI_Error rc = ini.SetLongValue("numbers", "hexval", 255, nullptr, true);
	ASSERT_EQ(rc, SI_INSERTED);

	std::string output;
	rc = ini.Save(output);
	ASSERT_EQ(rc, SI_OK);

	// Should be written as hex
	EXPECT_NE(output.find("0xff"), std::string::npos);

	// Should read back correctly
	long result = ini.GetLongValue("numbers", "hexval", 0);
	ASSERT_EQ(result, 255);
}

// Test SetLongValue with negative values
TEST_F(TestNumeric, TestSetLongValueNegative) {
	SI_Error rc = ini.SetLongValue("numbers", "negative", -9999);
	ASSERT_EQ(rc, SI_INSERTED);

	long result = ini.GetLongValue("numbers", "negative", 0);
	ASSERT_EQ(result, -9999);
}

// Test GetDoubleValue with valid doubles
TEST_F(TestNumeric, TestGetDoubleValue) {
	std::string input =
		"[floats]\n"
		"pi = 3.14159\n"
		"negative = -2.5\n"
		"integer = 42.0\n"
		"scientific = 1.23e-4\n"
		"zero = 0.0\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	double result = ini.GetDoubleValue("floats", "pi", 0.0);
	ASSERT_NEAR(result, 3.14159, 0.00001);

	result = ini.GetDoubleValue("floats", "negative", 0.0);
	ASSERT_NEAR(result, -2.5, 0.00001);

	result = ini.GetDoubleValue("floats", "integer", 0.0);
	ASSERT_NEAR(result, 42.0, 0.00001);

	result = ini.GetDoubleValue("floats", "scientific", 0.0);
	ASSERT_NEAR(result, 1.23e-4, 0.000001);

	result = ini.GetDoubleValue("floats", "zero", 1.0);
	ASSERT_NEAR(result, 0.0, 0.00001);
}

// Test GetDoubleValue with invalid values
TEST_F(TestNumeric, TestGetDoubleValueInvalid) {
	std::string input =
		"[floats]\n"
		"text = not_a_number\n"
		"empty = \n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	double result = ini.GetDoubleValue("floats", "text", 99.9);
	ASSERT_NEAR(result, 99.9, 0.00001);

	result = ini.GetDoubleValue("floats", "empty", 88.8);
	ASSERT_NEAR(result, 88.8, 0.00001);
}

// Test SetDoubleValue
TEST_F(TestNumeric, TestSetDoubleValue) {
	SI_Error rc = ini.SetDoubleValue("floats", "value1", 3.14159);
	ASSERT_EQ(rc, SI_INSERTED);

	double result = ini.GetDoubleValue("floats", "value1", 0.0);
	ASSERT_NEAR(result, 3.14159, 0.00001);

	// Update existing value
	rc = ini.SetDoubleValue("floats", "value1", 2.71828);
	ASSERT_EQ(rc, SI_UPDATED);

	result = ini.GetDoubleValue("floats", "value1", 0.0);
	ASSERT_NEAR(result, 2.71828, 0.00001);
}

// Test SetDoubleValue with negative and scientific notation
TEST_F(TestNumeric, TestSetDoubleValueFormats) {
	SI_Error rc = ini.SetDoubleValue("floats", "negative", -123.456);
	ASSERT_EQ(rc, SI_INSERTED);

	double result = ini.GetDoubleValue("floats", "negative", 0.0);
	ASSERT_NEAR(result, -123.456, 0.0001);

	rc = ini.SetDoubleValue("floats", "tiny", 0.000001);
	ASSERT_EQ(rc, SI_INSERTED);

	result = ini.GetDoubleValue("floats", "tiny", 0.0);
	ASSERT_NEAR(result, 0.000001, 0.0000001);
}

// Test multikey with numeric values
TEST_F(TestNumeric, TestMultikeyNumeric) {
	ini.SetMultiKey(true);

	std::string input =
		"[numbers]\n"
		"value = 10\n"
		"value = 20\n"
		"value = 30\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	bool hasMultiple = false;
	long result = ini.GetLongValue("numbers", "value", 0, &hasMultiple);
	ASSERT_EQ(result, 10);
	ASSERT_TRUE(hasMultiple);

	// Get all values
	CSimpleIniA::TNamesDepend values;
	ini.GetAllValues("numbers", "value", values);
	ASSERT_EQ(values.size(), 3);
}

// Test SetLongValue with force replace in multikey mode
TEST_F(TestNumeric, TestSetLongValueForceReplace) {
	ini.SetMultiKey(true);

	std::string input =
		"[numbers]\n"
		"value = 10\n"
		"value = 20\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	// Replace all values
	rc = ini.SetLongValue("numbers", "value", 999, nullptr, false, true);
	ASSERT_EQ(rc, SI_UPDATED);

	// Should only have one value now
	bool hasMultiple = false;
	long result = ini.GetLongValue("numbers", "value", 0, &hasMultiple);
	ASSERT_EQ(result, 999);
	ASSERT_FALSE(hasMultiple);
}

// Test extreme values
TEST_F(TestNumeric, TestExtremeValues) {
	SI_Error rc = ini.SetLongValue("numbers", "max", LONG_MAX);
	ASSERT_EQ(rc, SI_INSERTED);

	rc = ini.SetLongValue("numbers", "min", LONG_MIN);
	ASSERT_EQ(rc, SI_INSERTED);

	long result = ini.GetLongValue("numbers", "max", 0);
	ASSERT_EQ(result, LONG_MAX);

	result = ini.GetLongValue("numbers", "min", 0);
	ASSERT_EQ(result, LONG_MIN);
}

// Test numeric values with whitespace
TEST_F(TestNumeric, TestNumericWhitespace) {
	std::string input =
		"[numbers]\n"
		"padded =   42   \n"
		"tabs =\t123\t\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	long result = ini.GetLongValue("numbers", "padded", 0);
	ASSERT_EQ(result, 42);

	result = ini.GetLongValue("numbers", "tabs", 0);
	ASSERT_EQ(result, 123);
}

// Test numeric roundtrip
TEST_F(TestNumeric, TestNumericRoundtrip) {
	ini.SetLongValue("test", "long1", 12345);
	ini.SetLongValue("test", "long2", -67890);
	ini.SetDoubleValue("test", "double1", 3.14159);
	ini.SetDoubleValue("test", "double2", -2.71828);

	std::string output;
	SI_Error rc = ini.Save(output);
	ASSERT_EQ(rc, SI_OK);

	// Load it back
	CSimpleIniA ini2;
	ini2.SetUnicode();
	rc = ini2.LoadData(output);
	ASSERT_EQ(rc, SI_OK);

	ASSERT_EQ(ini2.GetLongValue("test", "long1", 0), 12345);
	ASSERT_EQ(ini2.GetLongValue("test", "long2", 0), -67890);
	ASSERT_NEAR(ini2.GetDoubleValue("test", "double1", 0.0), 3.14159, 0.00001);
	ASSERT_NEAR(ini2.GetDoubleValue("test", "double2", 0.0), -2.71828, 0.00001);
}