• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devmarkusb / util / 23399309665

22 Mar 2026 08:36AM UTC coverage: 88.255% (-0.002%) from 88.257%
23399309665

push

github

MarkusB
Refactor `new_statistics` to remove `UL_I_AM_SURE_TO_REPLACE_NEW_DELETE` macro and improve `str_convert` structure implementation.

15 of 15 new or added lines in 2 files covered. (100.0%)

5 existing lines in 3 files now uncovered.

5816 of 6590 relevant lines covered (88.25%)

347.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.11
/string/src/utf8.test.cpp
1
#include "ul/string/locale.h"
2
#include "utf8/checked.h"
3
#include "utf8/core.h"
4
#include "gtest/gtest.h"
5
#include <cstddef>
6
#include <cstdio>
7
#include <fstream>
8
#include <ios>
9
#include <iostream>
10
#include <iterator>
11
#include <stdexcept>
12
#include <string>
13

14
namespace ul = mb::ul;
15

16
namespace {
17
void save_to_text_file(const std::string& file_path_name_ext, const std::string& content) {
5✔
18
    std::ofstream file(file_path_name_ext);
5✔
19
    if (!file)
5✔
20
        throw std::runtime_error("file open");
×
21
    file << content;
5✔
22
}
5✔
23

24
void load_from_text_file(const std::string& file_path_name_ext, std::string& content) {
5✔
25
    std::ifstream file(file_path_name_ext);
5✔
26
    if (!file)
5✔
27
        throw std::runtime_error("file open");
×
28
    file.seekg(0, std::ios::end);
5✔
29
    if (!file)
5✔
30
        throw std::runtime_error("file seek");
×
31
    const auto size = file.tellg();
5✔
32
    if (!file || size == static_cast<decltype(size)>(-1))
5✔
33
        throw std::runtime_error("get file size");
×
34
    content.resize(static_cast<size_t>(size)); // need the precise size for the string, I guess
5✔
35
    file.seekg(0);
5✔
36
    if (!file)
5✔
37
        throw std::runtime_error("file seek");
×
38
    file.read(content.data(), size);
5✔
39
}
5✔
40
} // namespace
41

42
struct CharEncodingFileTest : public ::testing::Test {
43
    CharEncodingFileTest() = default;
5✔
44

45
    void SetUp() override {
5✔
46
        file_path_name_ext_ = "i_can_be_deleted__temp_txt_file_";
5✔
47
        file_path_name_ext_ += counter++;
5✔
48
        file_path_name_ext_ += ".txt";
5✔
49
    }
5✔
50

51
    void write_file(const std::string& text) const {
5✔
52
        save_to_text_file(file_path_name_ext_, text);
5✔
53
    }
5✔
54

55
    [[nodiscard]] std::string read_file() const {
5✔
56
        std::string ret;
5✔
57
        load_from_text_file(file_path_name_ext_, ret);
5✔
58
        return ret;
5✔
59
    }
×
60

61
    static bool is_valid_utf8(const std::string& s) {
5✔
62
        return utf8::is_valid(s.begin(), s.end());
5✔
63
    }
64

65
    void TearDown() override {
5✔
66
        const int ret = std::remove(file_path_name_ext_.c_str());
5✔
67
        ASSERT_EQ(0, ret);
5✔
68
    }
69

70
    ~CharEncodingFileTest() override = default;
5✔
71

72
private:
73
    std::string file_path_name_ext_;
74
    static inline char counter{'a'}; // NOLINT
75
};
76

77
TEST_F(CharEncodingFileTest, ASCII) {
4✔
78
    const std::string s = "!!AA~~";
1✔
79
    write_file(s);
1✔
80
    EXPECT_TRUE(is_valid_utf8(s));
1✔
81
    const std::string read = read_file();
2✔
82
    EXPECT_EQ(read, s);
1✔
83
}
1✔
84

85
TEST_F(CharEncodingFileTest, ASCIIhex) {
4✔
UNCOV
86
    const std::string s = "\x21\x21"
×
87
                          "AA~\x7e";
1✔
88
    EXPECT_TRUE(s == "!!AA~~");
1✔
89
    EXPECT_TRUE(is_valid_utf8(s));
1✔
90
    write_file(s);
1✔
91
    const std::string read = read_file();
2✔
92
    EXPECT_EQ(read, s);
1✔
93
}
1✔
94

95
TEST_F(CharEncodingFileTest, UTF8) {
4✔
UNCOV
96
    const std::string s = "\xc3\xa4"
×
97
                          "hnlich";
1✔
98
    EXPECT_TRUE(is_valid_utf8(s));
1✔
99
    write_file(s);
1✔
100
    const std::string read = read_file();
2✔
101
    EXPECT_EQ(read, s);
1✔
102
}
1✔
103

104
TEST_F(CharEncodingFileTest, UTF8_cyrillic) {
4✔
105
    const std::string s = "schtscha:\xd0\xa9";
1✔
106
    EXPECT_TRUE(is_valid_utf8(s));
1✔
107
    write_file(s);
1✔
108
    const std::string read = read_file();
2✔
109
    EXPECT_EQ(read, s);
1✔
110
}
1✔
111

112
TEST_F(CharEncodingFileTest, latin1) {
4✔
113
    const std::string s = "\xe4\xf6\xfc"; // latin1 (aeoeue)
1✔
114
    EXPECT_FALSE(is_valid_utf8(s));
1✔
115
    write_file(s);
1✔
116
    const std::string read = read_file();
2✔
117
    EXPECT_EQ(read, s);
1✔
118
}
1✔
119

120
TEST(consoleTest, utf8_to_utf16) {
4✔
UNCOV
121
    const std::string s = "\xc3\xa4"
×
122
                          "hnlich";
1✔
123
    std::cout << s << "\n";
1✔
124

125
    std::u16string utf16to;
1✔
126
    utf8::utf8to16(s.begin(), s.end(), std::back_inserter(utf16to));
1✔
127

128
    const std::wstring ws(utf16to.begin(), utf16to.end());
1✔
129

130
    // Cf. locale.test.cpp #locale_facet-exception for explanation.
131
#if !(UL_COMP_MINGW && UL_COMP_MINGW_VER <= 50300)
132
    const ul::SetGlobalLocaleScoped loc{ul::GlobalLocale::user_preferred};
1✔
133
#endif
134
    std::wcout << ws << L"\n";
1✔
135
    std::cout << s << "\n";
1✔
136
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc