~aleteoryx/muditaos

ref: 2695cc830472190ce1401869bb67f5506a3e17e6 muditaos/module-utils/test/test_time_conversion.cpp -rw-r--r-- 8.0 KiB
2695cc83 — piotrleniec-mudita [DW-31] Add commit message format checking on CI (#1201) 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <iostream>
#include <time/time_conversion.hpp>
#include <algorithm>
#include <thread.hpp>

namespace
{
    thread_local void *tls_pointers[configNUM_THREAD_LOCAL_STORAGE_POINTERS];
}

extern "C"
{

    void *ff_stdio_pvTaskGetThreadLocalStoragePointer(TaskHandle_t, BaseType_t xIndex)
    {
        return tls_pointers[xIndex];
    }
    void ff_stdio_vTaskSetThreadLocalStoragePointer(TaskHandle_t, BaseType_t xIndex, void *pvValue)
    {
        tls_pointers[xIndex] = pvValue;
    }
}

/// crap null stream from internets
/// (https://stackoverflow.com/questions/8243743/is-there-a-null-stdostream-implementation-in-c-or-libraries)
class NullStream : public std::ostream
{
    class NullBuffer : public std::streambuf
    {
      public:
        int overflow(int c)
        {
            return c;
        }
    } m_nb;

  public:
    NullStream() : std::ostream(&m_nb)
    {}
};

class vfs vfs;

using namespace utils::time;

using namespace std;

const map<string, time_t> test_times = {
    {"", 0},
};

void foo(UTF8 val)
{
    std::cout << "foo val: " << val << std::endl;
}

/// test if month and day shown by foo are the same as from strftime (with proper locale)
bool test_time_day_month_format(std::ostream &outstream)
{
    const time_t date        = 1569503186;
    const std::string format = "%a %A %b %B %Z %a <3 %I:%M %p %a";
    tm date_tp               = *localtime(&date);

    char c_data[128];
    strftime(c_data, 128, format.c_str(), &date_tp);
    /// we dont care about UTC+x etc. for now and dont know how will it be handled via settings?
    /// and where and for what, for tests sake remove CEST so that behaviour would be the same
    string pattern = "CEST";
    string data    = c_data;

    auto el = search(data.begin(), data.end(), pattern.begin(), pattern.end());
    if (el != data.end()) {
        data.erase(el, std::next(el, 4));
    }

    Time t(date);
    outstream << "\t"
              << "timestamp format:      " << format << std::endl;
    outstream << "\t"
              << "timestamp from format: " << t.str(format) << std::endl;
    outstream << "\t"
              << "strtof version:        " << data << std::endl;
    bool retval = true;
    if (!(t.str(format) == UTF8(data))) {
        std::cerr << "Data mismatch: " << std::endl
                  << "[" << t.str(format).length() << "]" << std::endl
                  << ">" << t.str(format) << "<" << std::endl
                  << " :vs: " << std::endl
                  << "[" << UTF8(data).length() << "]" << std::endl
                  << ">" << data << "<" << std::endl;
        retval = false;
    }

    outstream << "TEST: " << __FUNCTION__ << " result: " << retval << std::endl;
    return retval;
}

std::string fromtime(time_t time, std::string formatter)
{
    char cstr[128] = {0};
    tm ltm         = *localtime(&time);
    strftime(cstr, 128, formatter.c_str(), &ltm);
    return cstr;
}

bool test_time_date_format(std::ostream &outstream, std::string locale_format, std::string format, time_t time)
{
    bool retval = true;
    // prepare common point in time
    auto mytime = Timestamp();
    mytime.set_time(time);

    if (!(mytime.str(locale_format) == fromtime(time, format))) {
        std::cerr << "Format24HourMin errors: "
                  << "[" << fromtime(time, format).length() << "] " << fromtime(time, format) << " :vs: "
                  << "[" << mytime.str(locale_format).length() << "] " << mytime.str(locale_format) << std::endl;
        retval = false;
    }

    return retval;
}

bool test_time_format24hour_min(std::ostream &outstream)
{
    // this just shows usage, test is below
    // as converter uses str() method, this is equal to: constructor->str()->operator UTF8
    outstream << "\t"
              << "systime:            " << Timestamp() << std::endl;

    time_t sometime = 1569503186;
    bool retval     = test_time_date_format(outstream,
                                        Locale::format(Locale::TimeFormat::FormatTime24H),
                                        Locale::format(Locale::TimeFormat::FormatTime24H),
                                        sometime);

    outstream << "TEST: " << __FUNCTION__ << " result: " << retval << std::endl;
    return retval;
}

bool test_time_day(std::ostream &outstream)
{
    bool retval     = true;
    time_t sometime = 1569503186;
    auto mytime     = Timestamp();
    mytime.set_time(sometime);

    if (!(mytime.day() == fromtime(sometime, "%A"))) {
        std::cerr << "Day mismatch: " << std::endl
                  << mytime.day() << std::endl
                  << " :vs: " << std::endl
                  << fromtime(sometime, "%A") << std::endl;
        retval = false;
    }
    outstream << "TEST: " << __FUNCTION__ << " result: " << retval << std::endl;
    return retval;
}

bool test_time_day_abbrew(std::ostream &outstream)
{
    bool retval     = true;
    time_t sometime = 1569503186;
    auto mytime     = Timestamp();
    mytime.set_time(sometime);

    if (!(mytime.day(true) == fromtime(sometime, "%a"))) {
        std::cerr << "Day mismatch: " << std::endl
                  << mytime.day() << std::endl
                  << " :vs: " << std::endl
                  << fromtime(sometime, "%a") << std::endl;
        retval = false;
    }
    outstream << "TEST: " << __FUNCTION__ << " result: " << retval << std::endl;
    return retval;
}

//    outstream << "\t" << "systime, month:     " << mytime.month() << std::endl;
//    outstream << "\t" << "systime, month a:   " << mytime.month(true) << std::endl;

bool test_time_from_before(std::ostream &outstream, time_t before, std::string format_expected)
{
    auto retval   = true;
    auto mytime   = Timestamp(before);
    tm todaytime  = *localtime(&before);
    char buf[128] = {0};

    strftime(buf, 128, format_expected.c_str(), &todaytime);

    if (!(mytime.str(format_expected) == buf)) {
        std::cerr << "Error: "
                  << "\n\t" << mytime.str() << "\n\t"
                  << " :vs:"
                  << "\n\t" << buf << "\n\t"
                  << "format_expected: " << format_expected << "\n\t" << std::endl;
    }

    outstream << "TEST: " << __FUNCTION__ << " result: " << retval << std::endl;
    return retval;
}

bool test_time_from_today_200s(std::ostream &outstream)
{
    outstream << "TEST: " << __FUNCTION__ << std::endl;
    time_t timenow;
    bsp::rtc_GetCurrentTimestamp(&timenow);

    return test_time_from_before(outstream, timenow - 200, Locale::format(Locale::TimeFormat::FormatTime12H));
}

bool test_time_from_today_49h(std::ostream &outstream)
{
    outstream << "TEST: " << __FUNCTION__ << std::endl;
    time_t timenow;
    bsp::rtc_GetCurrentTimestamp(&timenow);

    return test_time_from_before(
        outstream, timenow - 3600 * 49, Locale::format(Locale::TimeFormat::FormatLocaleDateFull));
}

bool test_time_from_today_24h(std::ostream &outstream)
{
    outstream << "TEST: " << __FUNCTION__ << std::endl;
    time_t timenow;
    bsp::rtc_GetCurrentTimestamp(&timenow);

    return test_time_from_before(outstream, timenow - 3600 * 24, Locale::yesterday().c_str());
}

int main(int argc, char *argv[])
{
    time_t time_today = 0;

    vfs.Init();
    // get reference Today time
    if (bsp::rtc_GetCurrentTimestamp(&time_today)) {
        std::cerr << "Error on gettime" << std::endl;
        return -1;
    }

    // set locale to PL
    utils::localize.setDisplayLanguage("Polski");
    std::setlocale(LC_TIME, "pl_PL.UTF-8");

    // set default output to nullstream
    // change nullst to std::cout to debug
    // auto &nullst = std::cout;
    NullStream nullst;

    auto tests = {
        test_time_day_month_format,
        test_time_format24hour_min,
        test_time_day,
        test_time_day_abbrew,
        test_time_from_today_200s,
        test_time_from_today_24h,
        test_time_from_today_49h,
    };

    int retval = 0;
    for (auto test : tests) {
        if (!test(nullst)) {
            retval = 1;
        }
    }

    return retval;
}