~aleteoryx/muditaos

ref: 7d7003d62639426be8eb00f4fc288b68139f5566 muditaos/module-utils/test/test_utf8.cpp -rw-r--r-- 5.2 KiB
7d7003d6 — Marcin Smoczyński Merge branch 'master' into stable 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

/*
 * unittest_utf8.cpp
 *
 *  Created on: 29 kwi 2019
 *      Author: robert
 */

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

#include "utf8/UTF8.hpp"

const char *strings[] = {
    "TEST", "Rąbać", "I want to have pure phone", "Мой адрес: улица Тверская, дом пять, квартира семнадцать"};

const char *strings2[] = {
    "TEST\r", "Rąbać\n", "I want to have \rpure phone", "Мой адрес: улица Тверская, дом пять,\n квартира семнадцать"};

void test_raw_char_constructor(const char *str)
{
    UTF8 ustr = UTF8{str};
    cout << "    " << ustr.c_str() << " len: " << ustr.length() << " used bytes: " << ustr.used() << endl;
}

void test_string_constructor(const std::string &str)
{
    UTF8 ustr = UTF8{str};
    cout << "    " << ustr.c_str() << " len: " << ustr.length() << " used bytes: " << ustr.used() << endl;
}

void test_copy_constructor(const std::string &str)
{
    UTF8 bstr = UTF8{str};
    UTF8 ustr = UTF8{bstr};
    cout << "    " << ustr.c_str() << " len: " << ustr.length() << " used bytes: " << ustr.used() << endl;
}

void test_operator_index(const std::string &str)
{
    UTF8 ustr = UTF8{str};
    char s[4] = {0, 0, 0, 0};

    cout << "    ";
    for (uint32_t i = 0; i < ustr.length(); i++) {
        uint32_t value = ustr[i];
        memcpy(s, &value, 4);
        cout << s;
    }
    cout << endl;
}

void test_split(const std::string &str)
{
    UTF8 sourceString = UTF8{str};

    uint32_t divisionIdx = sourceString.length() / 2;

    cout << "    "
         << "Before split." << endl;
    cout << "        "
         << "srcString:    [" << sourceString.c_str() << "]    len: " << sourceString.length()
         << " used bytes: " << sourceString.used() << endl;
    char s[4]      = {0, 0, 0, 0};
    uint32_t value = sourceString[divisionIdx];
    memcpy(s, &value, 4);
    cout << "        "
         << "division character: [" << s << "] division index: " << divisionIdx << endl;

    UTF8 splitString = sourceString.split(divisionIdx);

    cout << "    "
         << "After split." << endl;
    cout << "        "
         << "srcString:    [" << sourceString.c_str() << "]    len: " << sourceString.length()
         << " used bytes: " << sourceString.used() << endl;
    cout << "        "
         << "sptString:    [" << splitString.c_str() << "]    len: " << splitString.length()
         << " used bytes: " << splitString.used() << endl;
    cout << "        "
         << "summary len: " << splitString.length() + sourceString.length()
         << " summary used bytes : " << splitString.used() + sourceString.used() << endl;
    cout << endl;
}
void test_getLine(const std::string &str)
{
    UTF8 ustr = UTF8{str};

    UTF8 dstr = ustr.getLine();

    cout << "    " << dstr.c_str() << endl;
}

void test_removeChar(const std::string &str, uint32_t i)
{
    UTF8 ustr(str);

    cout << "    Source: " << endl;
    cout << "    " << ustr.c_str() << " len: " << ustr.length() << " used bytes: " << ustr.used() << endl;
    // remove 'ś'
    bool retVal = ustr.removeChar(i, i * 2);
    cout << "    After remove: "
         << " remove pos: " << i << " remove count: " << i * 2 << endl;
    cout << "    " << ustr.c_str() << " len: " << ustr.length() << " used bytes: " << ustr.used()
         << " returned value: " << retVal << endl;
}

int main()
{

    cout << "RUNNING UNIT TEST FOR UTF8" << endl;

    cout << "UTF8( const char* str )" << endl;
    for (uint32_t i = 0; i < sizeof(strings) / sizeof(char *); ++i)
        test_raw_char_constructor(strings[i]);
    cout << endl;

    cout << "UTF8( const std::string& str )" << endl;
    for (uint32_t i = 0; i < sizeof(strings) / sizeof(char *); ++i)
        test_string_constructor(std::string{strings[i]});
    cout << endl;

    // copy constructor
    cout << "UTF8( const UTF8& utf )" << endl;
    for (uint32_t i = 0; i < sizeof(strings) / sizeof(char *); ++i) {
        test_copy_constructor(std::string{strings[i]});
    }
    cout << endl;

    cout << "uint32_t operator[]( const uint32_t& idx ) " << endl;
    for (uint32_t i = 0; i < sizeof(strings) / sizeof(char *); ++i) {
        test_operator_index(std::string{strings[i]});
    }
    cout << endl;

    cout << "UTF8 split( uint32_t idx ) " << endl;
    for (uint32_t i = 0; i < sizeof(strings) / sizeof(char *); ++i) {
        test_split(std::string{strings[i]});
    }
    cout << "UTF8 getLine( void ) " << endl;
    for (uint32_t i = 0; i < sizeof(strings2) / sizeof(char *); ++i) {
        test_getLine(std::string{strings2[i]});
    }
    cout << endl;
    cout << "bool removeChar(uint32_t pos, uint32_t count) " << endl;
    for (uint32_t i = 0; i < sizeof(strings) / sizeof(char *); ++i) {
        test_removeChar(std::string{strings[i]}, i);
    }
    cout << endl;

    UTF8 sourceString      = UTF8("Rąbać drewno siekierą");
    UTF8 destinationString = sourceString.substr(0, 5);

    cout << "UTF8 : " << '[' << sourceString.c_str() << ']' << endl;
    cout << "UTF8 substr: " << '[' << destinationString.c_str() << ']' << endl;

    cout << "" << endl; // prints
    return 0;
}