|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 2 | +// Copyright (c) 2026 ForestHub. All rights reserved. |
| 3 | +// For commercial licensing, visit https://github.com/ForestHubAI/fh-sdk |
| 4 | + |
| 5 | +#include "foresthub/util/strprintf.hpp" |
| 6 | + |
| 7 | +#include <gtest/gtest.h> |
| 8 | + |
| 9 | +#include <string> |
| 10 | + |
| 11 | +using foresthub::util::StrPrintf; |
| 12 | + |
| 13 | +// ========================================================================== |
| 14 | +// 1. Basic Formatting |
| 15 | +// ========================================================================== |
| 16 | + |
| 17 | +TEST(StrPrintfTest, EmptyFormatString) { |
| 18 | + EXPECT_EQ(StrPrintf(""), ""); |
| 19 | +} |
| 20 | + |
| 21 | +TEST(StrPrintfTest, PlainTextNoArgs) { |
| 22 | + EXPECT_EQ(StrPrintf("hello"), "hello"); |
| 23 | +} |
| 24 | + |
| 25 | +TEST(StrPrintfTest, StringArg) { |
| 26 | + EXPECT_EQ(StrPrintf("name: %s", "Alice"), "name: Alice"); |
| 27 | +} |
| 28 | + |
| 29 | +TEST(StrPrintfTest, IntArg) { |
| 30 | + EXPECT_EQ(StrPrintf("count: %d", 42), "count: 42"); |
| 31 | +} |
| 32 | + |
| 33 | +TEST(StrPrintfTest, FloatArg) { |
| 34 | + std::string result = StrPrintf("temp: %f", 3.14); |
| 35 | + EXPECT_EQ(result.substr(0, 10), "temp: 3.14"); |
| 36 | +} |
| 37 | + |
| 38 | +TEST(StrPrintfTest, MixedArgs) { |
| 39 | + EXPECT_EQ(StrPrintf("%s = %d", "x", 5), "x = 5"); |
| 40 | +} |
| 41 | + |
| 42 | +TEST(StrPrintfTest, PercentEscaping) { |
| 43 | + EXPECT_EQ(StrPrintf("100%%"), "100%"); |
| 44 | +} |
| 45 | + |
| 46 | +// ========================================================================== |
| 47 | +// 2. Type-Specific Formatting |
| 48 | +// ========================================================================== |
| 49 | + |
| 50 | +TEST(StrPrintfTest, NegativeInt) { |
| 51 | + EXPECT_EQ(StrPrintf("n: %d", -42), "n: -42"); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(StrPrintfTest, BoolAsInt) { |
| 55 | + EXPECT_EQ(StrPrintf("flag: %d", static_cast<int>(true)), "flag: 1"); |
| 56 | + EXPECT_EQ(StrPrintf("flag: %d", static_cast<int>(false)), "flag: 0"); |
| 57 | +} |
| 58 | + |
| 59 | +TEST(StrPrintfTest, FloatPrecision) { |
| 60 | + EXPECT_EQ(StrPrintf("val: %.2f", 3.14159), "val: 3.14"); |
| 61 | +} |
| 62 | + |
| 63 | +// ========================================================================== |
| 64 | +// 3. std::string Integration |
| 65 | +// ========================================================================== |
| 66 | + |
| 67 | +TEST(StrPrintfTest, StdStringCStr) { |
| 68 | + std::string name = "Bob"; |
| 69 | + EXPECT_EQ(StrPrintf("hello %s", name.c_str()), "hello Bob"); |
| 70 | +} |
| 71 | + |
| 72 | +TEST(StrPrintfTest, MultipleStringArgs) { |
| 73 | + std::string a = "foo"; |
| 74 | + std::string b = "bar"; |
| 75 | + EXPECT_EQ(StrPrintf("%s und Kontext: %s", a.c_str(), b.c_str()), "foo und Kontext: bar"); |
| 76 | +} |
| 77 | + |
| 78 | +// ========================================================================== |
| 79 | +// 4. Large Strings |
| 80 | +// ========================================================================== |
| 81 | + |
| 82 | +TEST(StrPrintfTest, LargeString) { |
| 83 | + std::string payload(2048, 'X'); |
| 84 | + std::string result = StrPrintf("data: %s", payload.c_str()); |
| 85 | + EXPECT_EQ(result.size(), 6 + 2048); // "data: " + payload |
| 86 | + EXPECT_EQ(result.substr(0, 6), "data: "); |
| 87 | + EXPECT_EQ(result.back(), 'X'); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(StrPrintfTest, LargeFormatResult) { |
| 91 | + // Simulate RAG-style output >4KB |
| 92 | + std::string ctx(4096, 'A'); |
| 93 | + std::string serial(1024, 'B'); |
| 94 | + std::string result = StrPrintf("%s und Kontext: %s", serial.c_str(), ctx.c_str()); |
| 95 | + EXPECT_EQ(result.size(), 1024 + 14 + 4096); // serial + " und Kontext: " + ctx |
| 96 | +} |
0 commit comments