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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
#include "../SimpleIni.h"
#include "gtest/gtest.h"
#include <algorithm>
class TestEdgeCases : public ::testing::Test {
protected:
void SetUp() override;
protected:
CSimpleIniA ini;
};
void TestEdgeCases::SetUp() {
ini.SetUnicode();
}
// Test special characters in section names
TEST_F(TestEdgeCases, TestSpecialCharsSectionNames) {
std::string input =
"[section-with-dashes]\n"
"key = value1\n"
"\n"
"[section_with_underscores]\n"
"key = value2\n"
"\n"
"[section.with.dots]\n"
"key = value3\n"
"\n"
"[section:with:colons]\n"
"key = value4\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_STREQ(ini.GetValue("section-with-dashes", "key"), "value1");
ASSERT_STREQ(ini.GetValue("section_with_underscores", "key"), "value2");
ASSERT_STREQ(ini.GetValue("section.with.dots", "key"), "value3");
ASSERT_STREQ(ini.GetValue("section:with:colons", "key"), "value4");
}
// Test special characters in key names
TEST_F(TestEdgeCases, TestSpecialCharsKeyNames) {
std::string input =
"[section]\n"
"key-with-dashes = value1\n"
"key_with_underscores = value2\n"
"key.with.dots = value3\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_STREQ(ini.GetValue("section", "key-with-dashes"), "value1");
ASSERT_STREQ(ini.GetValue("section", "key_with_underscores"), "value2");
ASSERT_STREQ(ini.GetValue("section", "key.with.dots"), "value3");
}
// Test equals sign in value
TEST_F(TestEdgeCases, TestEqualsInValue) {
std::string input =
"[section]\n"
"key1 = value=with=equals\n"
"key2 = a=b\n"
"key3 = ===\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_STREQ(ini.GetValue("section", "key1"), "value=with=equals");
ASSERT_STREQ(ini.GetValue("section", "key2"), "a=b");
ASSERT_STREQ(ini.GetValue("section", "key3"), "===");
}
// Test semicolon in value (comment character)
TEST_F(TestEdgeCases, TestSemicolonInValue) {
std::string input =
"[section]\n"
"key = value ; this is not a comment\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// The semicolon and text after should be part of value (trimmed)
const char* value = ini.GetValue("section", "key");
ASSERT_NE(value, nullptr);
// Value might be "value ; this is not a comment" or just "value" depending on implementation
}
// Test hash in value (comment character)
TEST_F(TestEdgeCases, TestHashInValue) {
std::string input =
"[section]\n"
"key = value # this is not a comment\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
const char* value = ini.GetValue("section", "key");
ASSERT_NE(value, nullptr);
}
// Test bracket in value
TEST_F(TestEdgeCases, TestBracketInValue) {
std::string input =
"[section]\n"
"key1 = [value]\n"
"key2 = ]value[\n"
"key3 = [[nested]]\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_STREQ(ini.GetValue("section", "key1"), "[value]");
ASSERT_STREQ(ini.GetValue("section", "key2"), "]value[");
ASSERT_STREQ(ini.GetValue("section", "key3"), "[[nested]]");
}
// Test very long section name
TEST_F(TestEdgeCases, TestLongSectionName) {
std::string longName(1000, 'a');
std::string input = "[" + longName + "]\nkey=value\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_TRUE(ini.SectionExists(longName.c_str()));
ASSERT_STREQ(ini.GetValue(longName.c_str(), "key"), "value");
}
// Test very long key name
TEST_F(TestEdgeCases, TestLongKeyName) {
std::string longKey(1000, 'b');
std::string input = "[section]\n" + longKey + "=value\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_STREQ(ini.GetValue("section", longKey.c_str()), "value");
}
// Test very long value
TEST_F(TestEdgeCases, TestLongValue) {
std::string longValue(10000, 'c');
ini.SetValue("section", "key", longValue.c_str());
const char* value = ini.GetValue("section", "key");
ASSERT_STREQ(value, longValue.c_str());
}
// Test leading whitespace in section name
TEST_F(TestEdgeCases, TestLeadingWhitespaceSection) {
std::string input =
"[ section ]\n"
"key = value\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// Whitespace should be trimmed
ASSERT_TRUE(ini.SectionExists("section"));
ASSERT_STREQ(ini.GetValue("section", "key"), "value");
}
// Test leading/trailing whitespace in key name
TEST_F(TestEdgeCases, TestWhitespaceKeyName) {
std::string input =
"[section]\n"
" key = value\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// Whitespace should be trimmed
ASSERT_TRUE(ini.KeyExists("section", "key"));
ASSERT_STREQ(ini.GetValue("section", "key"), "value");
}
// Test leading/trailing whitespace in value
TEST_F(TestEdgeCases, TestWhitespaceValue) {
std::string input =
"[section]\n"
"key = value \n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// Whitespace should be trimmed
const char* value = ini.GetValue("section", "key");
ASSERT_STREQ(value, "value");
}
// Test tabs as whitespace
TEST_F(TestEdgeCases, TestTabWhitespace) {
std::string input =
"[\tsection\t]\n"
"\tkey\t=\tvalue\t\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_TRUE(ini.SectionExists("section"));
ASSERT_STREQ(ini.GetValue("section", "key"), "value");
}
// Test empty lines and multiple blank lines
TEST_F(TestEdgeCases, TestEmptyLines) {
std::string input =
"\n\n\n"
"[section1]\n"
"\n\n"
"key1 = value1\n"
"\n\n\n"
"[section2]\n"
"\n"
"key2 = value2\n"
"\n\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_STREQ(ini.GetValue("section1", "key1"), "value1");
ASSERT_STREQ(ini.GetValue("section2", "key2"), "value2");
}
// Test different newline formats
TEST_F(TestEdgeCases, TestMixedNewlines) {
// Mix of \r\n, \n, and \r
std::string input =
"[section1]\r\n"
"key1 = value1\n"
"[section2]\r"
"key2 = value2\r\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
ASSERT_STREQ(ini.GetValue("section1", "key1"), "value1");
ASSERT_STREQ(ini.GetValue("section2", "key2"), "value2");
}
// Test malformed section (no closing bracket)
TEST_F(TestEdgeCases, TestMalformedSection) {
std::string input =
"[section\n"
"key = value\n";
SI_Error rc = ini.LoadData(input);
// Should either handle gracefully or accept as-is
ASSERT_EQ(rc, SI_OK);
}
// Test multiple equals signs on line
TEST_F(TestEdgeCases, TestMultipleEquals) {
std::string input =
"[section]\n"
"key = value = more = data\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// Everything after first = should be the value
ASSERT_STREQ(ini.GetValue("section", "key"), "value = more = data");
}
// Test empty value vs no equals
TEST_F(TestEdgeCases, TestEmptyVsNoEquals) {
ini.SetAllowKeyOnly(true);
std::string input =
"[section]\n"
"key1 = \n"
"key2\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
const char* val1 = ini.GetValue("section", "key1");
const char* val2 = ini.GetValue("section", "key2");
// Both should exist
ASSERT_TRUE(ini.KeyExists("section", "key1"));
ASSERT_TRUE(ini.KeyExists("section", "key2"));
// key1 should have empty string, key2 should be NULL or empty
ASSERT_NE(val1, nullptr);
ASSERT_STREQ(val1, "");
}
// Test Unicode section/key/value names
TEST_F(TestEdgeCases, TestUnicode) {
const char tesuto[] = u8"テスト";
const char kensa[] = u8"検査";
const char value[] = u8"値";
ini.SetValue(tesuto, kensa, value);
const char* result = ini.GetValue(tesuto, kensa);
ASSERT_STREQ(result, value);
}
// Test many sections
TEST_F(TestEdgeCases, TestManySections) {
for (int i = 0; i < 1000; i++) {
std::string section = "section" + std::to_string(i);
ini.SetValue(section.c_str(), "key", "value");
}
CSimpleIniA::TNamesDepend sections;
ini.GetAllSections(sections);
ASSERT_EQ(sections.size(), 1000);
// Verify a few
ASSERT_STREQ(ini.GetValue("section0", "key"), "value");
ASSERT_STREQ(ini.GetValue("section500", "key"), "value");
ASSERT_STREQ(ini.GetValue("section999", "key"), "value");
}
// Test many keys in one section
TEST_F(TestEdgeCases, TestManyKeys) {
for (int i = 0; i < 1000; i++) {
std::string key = "key" + std::to_string(i);
ini.SetValue("section", key.c_str(), "value");
}
ASSERT_EQ(ini.GetSectionSize("section"), 1000);
// Verify a few
ASSERT_STREQ(ini.GetValue("section", "key0"), "value");
ASSERT_STREQ(ini.GetValue("section", "key500"), "value");
ASSERT_STREQ(ini.GetValue("section", "key999"), "value");
}
// Test SetValue with comment
TEST_F(TestEdgeCases, TestSetValueWithComment) {
SI_Error rc = ini.SetValue("section", "key", "value", "; This is a comment");
ASSERT_EQ(rc, SI_INSERTED);
ASSERT_STREQ(ini.GetValue("section", "key"), "value");
std::string output;
ini.Save(output);
// Comment should be in output
EXPECT_NE(output.find("; This is a comment"), std::string::npos);
}
// Test key without section (should go to empty section)
TEST_F(TestEdgeCases, TestKeyWithoutSection) {
std::string input =
"key1 = value1\n"
"\n"
"[section]\n"
"key2 = value2\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// key1 should be in empty section
ASSERT_STREQ(ini.GetValue("", "key1"), "value1");
ASSERT_STREQ(ini.GetValue("section", "key2"), "value2");
}
// Test consecutive sections with same name (should merge or override)
TEST_F(TestEdgeCases, TestDuplicateSections) {
std::string input =
"[section]\n"
"key1 = value1\n"
"\n"
"[section]\n"
"key2 = value2\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// Both keys should be in the section
ASSERT_STREQ(ini.GetValue("section", "key1"), "value1");
ASSERT_STREQ(ini.GetValue("section", "key2"), "value2");
ASSERT_EQ(ini.GetSectionSize("section"), 2);
}
// Test value with only whitespace
TEST_F(TestEdgeCases, TestWhitespaceOnlyValue) {
std::string input =
"[section]\n"
"key1 = \n"
"key2 = \t\t\t\n";
SI_Error rc = ini.LoadData(input);
ASSERT_EQ(rc, SI_OK);
// Whitespace-only should be trimmed to empty
const char* val1 = ini.GetValue("section", "key1");
const char* val2 = ini.GetValue("section", "key2");
ASSERT_NE(val1, nullptr);
ASSERT_NE(val2, nullptr);
ASSERT_STREQ(val1, "");
ASSERT_STREQ(val2, "");
}
|