GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 0 / 71 / 71
Functions: -% 0 / 18 / 18
Branches: -% 0 / 612 / 612

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