test/unittest/UnitCommonUtil.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | // LCOV_EXCL_START | ||
| 3 | |||
| 4 | #include <gtest/gtest.h> | ||
| 5 | |||
| 6 | #include <util/CommonUtil.h> | ||
| 7 | |||
| 8 | namespace spice::testing { | ||
| 9 | |||
| 10 | using namespace spice::compiler; | ||
| 11 | |||
| 12 | template <typename T> | ||
| 13 | struct DummyStruct { | ||
| 14 | T t; | ||
| 15 | }; | ||
| 16 | |||
| 17 | − | TEST(CommonUtilTest, ReplaceAll) { | |
| 18 | − | std::string test = "This is a test"; | |
| 19 | − | CommonUtil::replaceAll(test, " ", "_"); | |
| 20 | − | ASSERT_EQ("This_is_a_test", test); | |
| 21 | |||
| 22 | − | test = "This is a test"; | |
| 23 | − | CommonUtil::replaceAll(test, "is", "was"); | |
| 24 | − | ASSERT_EQ("Thwas was a test", test); | |
| 25 | |||
| 26 | − | test = "This is a test"; | |
| 27 | − | CommonUtil::replaceAll(test, "is", "is"); | |
| 28 | − | ASSERT_EQ("This is a test", test); | |
| 29 | |||
| 30 | − | test = "This is a test"; | |
| 31 | − | CommonUtil::replaceAll(test, "is", "is"); | |
| 32 | − | ASSERT_EQ("This is a test", test); | |
| 33 | |||
| 34 | − | test = "This is a test"; | |
| 35 | − | CommonUtil::replaceAll(test, "is", "is"); | |
| 36 | − | ASSERT_EQ("This is a test", test); | |
| 37 | |||
| 38 | − | test = "This is a test"; | |
| 39 | − | CommonUtil::replaceAll(test, "is", "is"); | |
| 40 | − | ASSERT_EQ("This is a test", test); | |
| 41 | − | } | |
| 42 | |||
| 43 | − | TEST(CommonUtilTest, GetLastFragment) { | |
| 44 | − | ASSERT_EQ("test", CommonUtil::getLastFragment("This is a test", " ")); | |
| 45 | − | ASSERT_EQ("test", CommonUtil::getLastFragment("This_is_a_test", "_")); | |
| 46 | − | ASSERT_EQ("", CommonUtil::getLastFragment("This.is.a.", ".")); | |
| 47 | − | ASSERT_EQ("This is a test", CommonUtil::getLastFragment("This is a test", "This is a test ")); | |
| 48 | } | ||
| 49 | |||
| 50 | − | TEST(CommonUtilTest, Trim) { | |
| 51 | − | ASSERT_EQ("test", CommonUtil::trim(" test ")); | |
| 52 | − | ASSERT_EQ("This is a test", CommonUtil::trim("This is a test ")); | |
| 53 | − | ASSERT_EQ("String with whitespaces only at the front", CommonUtil::trim(" String with whitespaces only at the front")); | |
| 54 | − | ASSERT_EQ("test", CommonUtil::trim("test")); | |
| 55 | − | ASSERT_EQ(" ", CommonUtil::trim(" ")); | |
| 56 | − | ASSERT_EQ("", CommonUtil::trim("")); | |
| 57 | } | ||
| 58 | |||
| 59 | − | TEST(CommonUtilTest, Split) { | |
| 60 | − | ASSERT_EQ(std::vector<std::string>({"test", "test"}), CommonUtil::split("test test")); | |
| 61 | − | ASSERT_EQ(std::vector<std::string>({"This", "is", "a", "test"}), CommonUtil::split("This is a test")); | |
| 62 | − | ASSERT_EQ(std::vector<std::string>({"String", "with", "whitespaces", "at", "the", "front"}), | |
| 63 | − | CommonUtil::split(" String with whitespaces at the front")); | |
| 64 | − | ASSERT_EQ(std::vector<std::string>({"test"}), CommonUtil::split("test")); | |
| 65 | − | ASSERT_EQ(std::vector<std::string>({"", ""}), CommonUtil::split(" ")); | |
| 66 | − | ASSERT_EQ(std::vector<std::string>({}), CommonUtil::split("")); | |
| 67 | } | ||
| 68 | |||
| 69 | − | TEST(CommonUtilTest, FormatBytes) { | |
| 70 | − | ASSERT_EQ("0.00 B", CommonUtil::formatBytes(0ull)); | |
| 71 | − | ASSERT_EQ("1.00 B", CommonUtil::formatBytes(1ull)); | |
| 72 | − | ASSERT_EQ("1.00 KB", CommonUtil::formatBytes(1024ull)); | |
| 73 | − | ASSERT_EQ("1.00 MB", CommonUtil::formatBytes(1024ull * 1024ull)); | |
| 74 | − | ASSERT_EQ("3.45 MB", CommonUtil::formatBytes(1024ull * 1024ull * 3.45)); | |
| 75 | − | ASSERT_EQ("1.00 GB", CommonUtil::formatBytes(1024ull * 1024ull * 1024ull)); | |
| 76 | − | ASSERT_EQ("9.92 GB", CommonUtil::formatBytes(1024ull * 1024ull * 1024ull * 9.92)); | |
| 77 | − | ASSERT_EQ("1.00 TB", CommonUtil::formatBytes(1024ull * 1024ull * 1024ull * 1024ull)); | |
| 78 | } | ||
| 79 | |||
| 80 | − | TEST(CommonUtilTest, DemangleTypeName) { | |
| 81 | // Successful cases | ||
| 82 | − | ASSERT_EQ("int", CommonUtil::demangleTypeName(typeid(int).name())); | |
| 83 | − | ASSERT_EQ("spice::testing::DummyStruct<double>", CommonUtil::demangleTypeName(typeid(DummyStruct<double>).name())); | |
| 84 | // Mangled name is returned, if de-mangling was not successful | ||
| 85 | − | ASSERT_EQ("5TestIdE", CommonUtil::demangleTypeName("5TestIdE")); | |
| 86 | } | ||
| 87 | |||
| 88 | } // namespace spice::testing | ||
| 89 | |||
| 90 | // LCOV_EXCL_STOP | ||
| 91 |