From c9522fb740200ccef6230cec452c48efb31e5394 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 11 May 2023 22:05:17 +0200 Subject: Removed all Printf-family functions from StringUtils. Replaced them with fmt::format calls, including changes to the format strings. Also changed the format strings to use FMT_STRING, so that the format is checked compile-time against the arguments. Also fixed code-style violations already present in the code. --- tests/BlockTypeRegistry/PalettedBlockAreaTest.cpp | 12 ++++---- tests/Generating/BasicGeneratorTest.cpp | 21 +++++++------ tests/TestHelpers.h | 37 +++++++++++++---------- 3 files changed, 38 insertions(+), 32 deletions(-) (limited to 'tests') diff --git a/tests/BlockTypeRegistry/PalettedBlockAreaTest.cpp b/tests/BlockTypeRegistry/PalettedBlockAreaTest.cpp index ba4967b35..506d5ccf7 100644 --- a/tests/BlockTypeRegistry/PalettedBlockAreaTest.cpp +++ b/tests/BlockTypeRegistry/PalettedBlockAreaTest.cpp @@ -94,7 +94,7 @@ Used to be a function, but clang-3.5 didn't like it ("error: debug information f { \ for (int z = 0; z < 5; ++z) \ { \ - pbaA.setBlock({x, y, z}, Printf("A-%d-%d-%d", x, y, z), BlockState()); \ + pbaA.setBlock({x, y, z}, fmt::format(FMT_STRING("A-{}-{}-{}"), x, y, z), BlockState()); \ } \ } \ } \ @@ -105,7 +105,7 @@ Used to be a function, but clang-3.5 didn't like it ("error: debug information f { \ for (int z = 0; z < 6; ++z) \ { \ - pbaB.setBlock({x, y, z}, Printf("B-%d-%d-%d", x, y, z), BlockState()); \ + pbaB.setBlock({x, y, z}, fmt::format(FMT_STRING("B-{}-{}-{}"), x, y, z), BlockState()); \ } \ } \ } \ @@ -217,7 +217,7 @@ static void testPastingCompletelyInside() TEST_EQUAL_MSG( pbaA.block({x, y, z}).first, expected[z][y][x], - Printf("{%d, %d, %d}, exp %s, got %s", x, y, z, expected[z][y][x].c_str(), pbaA.block({x, y, z}).first.c_str()).c_str() + fmt::format(FMT_STRING("{{{}, {}, {}}}, exp {}, got {}"), x, y, z, expected[z][y][x], pbaA.block({x, y, z}).first) ); } } @@ -282,7 +282,7 @@ static void testPastingPositiveOverflow() TEST_EQUAL_MSG( pbaA.block({x, y, z}).first, expected[z][y][x], - Printf("{%d, %d, %d}, exp %s, got %s", x, y, z, expected[z][y][x].c_str(), pbaA.block({x, y, z}).first.c_str()).c_str() + fmt::format(FMT_STRING("{{{}, {}, {}}}, exp {}, got {}"), x, y, z, expected[z][y][x], pbaA.block({x, y, z}).first) ); } } @@ -347,7 +347,7 @@ static void testPastingNegativeOverflow() TEST_EQUAL_MSG( pbaA.block({x, y, z}).first, expected[z][y][x], - Printf("{%d, %d, %d}, exp %s, got %s", x, y, z, expected[z][y][x].c_str(), pbaA.block({x, y, z}).first.c_str()).c_str() + fmt::format(FMT_STRING("{{{}, {}, {}}}, exp {}, got {}"), x, y, z, expected[z][y][x], pbaA.block({x, y, z}).first) ); } } @@ -412,7 +412,7 @@ static void testPastingMixedOverflow() TEST_EQUAL_MSG( pbaA.block({x, y, z}).first, expected[z][y][x], - Printf("{%d, %d, %d}, exp %s, got %s", x, y, z, expected[z][y][x].c_str(), pbaA.block({x, y, z}).first.c_str()).c_str() + fmt::format(FMT_STRING("{{{}, {}, {}}}, exp {}, got {}"), x, y, z, expected[z][y][x], pbaA.block({x, y, z}).first) ); } } diff --git a/tests/Generating/BasicGeneratorTest.cpp b/tests/Generating/BasicGeneratorTest.cpp index 10c66050b..7b35b0ee5 100644 --- a/tests/Generating/BasicGeneratorTest.cpp +++ b/tests/Generating/BasicGeneratorTest.cpp @@ -22,7 +22,7 @@ static void verifyChunkDescHeightmap(const cChunkDesc & a_ChunkDesc) if (BlockType != E_BLOCK_AIR) { int Height = a_ChunkDesc.GetHeight(x, z); - TEST_EQUAL_MSG(Height, y, Printf("Chunk height at <%d, %d>: exp %d, got %d", x, z, y, Height)); + TEST_EQUAL_MSG(Height, y, fmt::format(FMT_STRING("Chunk height at <{}, {}>: exp {}, got {}"), x, z, y, Height)); break; } } // for y @@ -99,7 +99,7 @@ static void testGenerateOverworld(cChunkGenerator & aDefaultOverworldGen) { for (int z = 0; z < cChunkDef::Width; ++z) { - TEST_EQUAL_MSG(chd.GetBlockType(x, 0, z), E_BLOCK_BEDROCK, Printf("Bedrock floor at {%d, 0, %d}", x, z)); + TEST_EQUAL_MSG(chd.GetBlockType(x, 0, z), E_BLOCK_BEDROCK, fmt::format(FMT_STRING("Bedrock floor at {{{}, {}, {}}}"), x, 0, z)); } } @@ -124,12 +124,12 @@ static void testGenerateOverworld(cChunkGenerator & aDefaultOverworldGen) auto y = chd.GetHeight(x, z); auto blockType = chd.GetBlockType(x, y, z); TEST_EQUAL_MSG(validOverworldBlockTypes.count(blockType), 1, - Printf("Block at {%d, %d, %d}: %d", x, y, z, blockType) + fmt::format(FMT_STRING("Block at {{{}, {}, {}}}: {}"), x, y, z, blockType) ); if (y < cChunkDef::Height - 1) { TEST_EQUAL_MSG(chd.GetBlockType(x, cChunkDef::Height - 1, z), E_BLOCK_AIR, - Printf("Air at {%d, %d, %d}", x, cChunkDef::Height - 1, z) + fmt::format(FMT_STRING("Air at {{{}, {}, {}}}"), x, cChunkDef::Height - 1, z) ); } } @@ -163,7 +163,7 @@ static void testGenerateNether(cChunkGenerator & aDefaultNetherGen) { for (int z = 0; z < cChunkDef::Width; ++z) { - TEST_EQUAL_MSG(chd.GetBiome(x, z), biNether, Printf("Nether biome at {%d, %d}", x, z)); + TEST_EQUAL_MSG(chd.GetBiome(x, z), biNether, fmt::format(FMT_STRING("Nether biome at <{}, {}>"), x, z)); } } @@ -173,7 +173,7 @@ static void testGenerateNether(cChunkGenerator & aDefaultNetherGen) { for (int z = 0; z < cChunkDef::Width; ++z) { - TEST_EQUAL_MSG(chd.GetBlockType(x, 0, z), E_BLOCK_BEDROCK, Printf("Bedrock floor at {%d, 0, %d}", x, z)); + TEST_EQUAL_MSG(chd.GetBlockType(x, 0, z), E_BLOCK_BEDROCK, fmt::format(FMT_STRING("Bedrock floor at {{{}, {}, {}}}"), x, 0, z)); auto y = chd.GetHeight(x, z); auto topBlockType = chd.GetBlockType(x, y, z); // Skip the mushrooms generated on the top bedrock layer: @@ -184,12 +184,13 @@ static void testGenerateNether(cChunkGenerator & aDefaultNetherGen) { y -= 1; } - TEST_EQUAL_MSG(y, prevHeight, Printf("Failed: Same height across the entire chunk, at {%d, %d}: exp %d, got %d; top block: %d", + TEST_EQUAL_MSG(y, prevHeight, fmt::format( + FMT_STRING("Failed: Same height across the entire chunk, at <{}, {}>: exp {}, got {}; top block: {}"), x, z, prevHeight, y, chd.GetBlockType(x, y, z) )); auto blockType = chd.GetBlockType(x, y, z); TEST_EQUAL_MSG(blockType, E_BLOCK_BEDROCK, - Printf("Bedrock ceiling at {%d, %d, %d}: %d", x, y, z, blockType) + fmt::format(FMT_STRING("Bedrock ceiling at {{{}, {}, {}}}: {}"), x, y, z, blockType) ); } } @@ -218,7 +219,7 @@ static void testGenerateNether(cChunkGenerator & aDefaultNetherGen) if (!hasSuitableBlockType) { printChunkColumn(chd, x, z); - TEST_FAIL(Printf("!hasSuitableBlockType at column {%d, %d} of chunk [%d, 0]", x, z, chunkX)); + TEST_FAIL(fmt::format(FMT_STRING("!hasSuitableBlockType at column <{}, {}> of chunk [{}, 0]"), x, z, chunkX)); } } } @@ -263,7 +264,7 @@ static void checkChunkChecksums( */ auto checksum = chunkSHA1(chd); TEST_EQUAL_MSG(checksum, coords.mChecksum, - Printf("%s chunk %s SHA1: expected %s, got %s", aDimension, coords.mCoords.ToString(), coords.mChecksum, checksum) + fmt::format(FMT_STRING("{} chunk {} SHA1: expected {}, got {}"), aDimension, coords.mCoords.ToString(), coords.mChecksum, checksum) ); } } diff --git a/tests/TestHelpers.h b/tests/TestHelpers.h index cd1a52998..665d4bf52 100644 --- a/tests/TestHelpers.h +++ b/tests/TestHelpers.h @@ -18,6 +18,7 @@ IMPLEMENT_TEST_MAIN("TestName", + /** The exception that is thrown if a test fails. It doesn't inherit from any type so that it is not possible to catch it by a mistake, it needs to be caught explicitly (used in the TEST_THROWS). @@ -50,9 +51,10 @@ public: { \ throw TestException( \ __FILE__, __LINE__, __FUNCTION__, \ - Printf("Equality test failed: %s != %s", #VAL1, #VAL2)); \ + fmt::format(FMT_STRING("Equality test failed: {} != {}"), #VAL1, #VAL2) \ + ); \ } \ - } while (false) + } while (false) @@ -65,7 +67,8 @@ public: { \ throw TestException( \ __FILE__, __LINE__, __FUNCTION__, \ - Printf("Equality test failed: %s != %s (%s)", #VAL1, #VAL2, MSG)); \ + fmt::format(FMT_STRING("Equality test failed: {} != {} ({})"), #VAL1, #VAL2, MSG) \ + ); \ } \ } while (false) @@ -80,10 +83,10 @@ public: { \ throw TestException( \ __FILE__, __LINE__, __FUNCTION__, \ - Printf("Inequality test failed: %s == %s", #VAL1, #VAL2) \ - ); \ + fmt::format(FMT_STRING("Inequality test failed: {} == {}"), #VAL1, #VAL2) \ + ); \ } \ - } while(false) + } while (false) @@ -108,7 +111,7 @@ public: do { \ if (Stmt < Val) \ { \ - throw TestException(__FILE__, __LINE__, __FUNCTION__, Printf("Comparison failed: %s < %s", #Stmt, #Val)); \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, fmt::format(FMT_STRING("Comparison failed: {} < {}"), #Stmt, #Val)); \ } \ } while (false) @@ -121,7 +124,7 @@ public: do { \ if (Stmt > Val) \ { \ - throw TestException(__FILE__, __LINE__, __FUNCTION__, Printf("Comparison failed: %s > %s", #Stmt, #Val)); \ + throw TestException(__FILE__, __LINE__, __FUNCTION__, fmt::format(FMT_STRING("Comparison failed: {} > {}"), #Stmt, #Val)); \ } \ } while (false) @@ -137,8 +140,8 @@ public: Stmt; \ throw TestException( \ __FILE__, __LINE__, __FUNCTION__, \ - Printf("Failed to throw an exception of type %s", #ExcClass) \ - ); \ + fmt::format(FMT_STRING("Failed to throw an exception of type {}"), #ExcClass) \ + ); \ } \ catch (const ExcClass &) \ { \ @@ -148,17 +151,18 @@ public: { \ throw TestException( \ __FILE__, __LINE__, __FUNCTION__, \ - Printf("An unexpected std::exception descendant was thrown, was expecting type %s. Message is: %s", \ + fmt::format( \ + FMT_STRING("An unexpected std::exception descendant was thrown, was expecting type {}. Message is: {}"), \ #ExcClass, exc.what() \ - )); \ + ) \ + ); \ } \ catch (...)\ { \ throw TestException( \ __FILE__, __LINE__, __FUNCTION__, \ - Printf("An unexpected unknown exception object was thrown, was expecting type %s", \ - #ExcClass \ - )); \ + fmt::format(FMT_STRING("An unexpected unknown exception object was thrown, was expecting type {}"), #ExcClass) \ + ); \ } \ } while (false) @@ -174,7 +178,8 @@ public: Stmt; \ throw TestException( \ __FILE__, __LINE__, __FUNCTION__, \ - "Failed to throw an exception of any type"); \ + "Failed to throw an exception of any type" \ + ); \ } \ catch (const TestException & exc) \ { \ -- cgit v1.2.3