File: number_locale_test.cpp

package info (click to toggle)
wf-config 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: cpp: 5,743; xml: 37; makefile: 4
file content (156 lines) | stat: -rw-r--r-- 4,625 bytes parent folder | download | duplicates (2)
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
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include <wayfire/config/types.hpp>
#include <linux/input-event-codes.h>
#include <limits>

#include <locale>

#define WF_CONFIG_DOUBLE_EPS 0.01

using namespace wf;
using namespace wf::option_type;


void setup_test_locale()
{
    // requires glibc-langpack-de
    // changes std::to_string output
    std::setlocale(LC_NUMERIC, "fr_FR.UTF-8");
}

TEST_CASE("wf::option_type::to_string<double>")
{
    setup_test_locale();

    std::string str = wf::option_type::to_string(3.14);
    CHECK(str == "3.140000");
    CHECK(wf::option_type::to_string(3140) == "3140");
}

TEST_CASE("wf::bool_wrapper_t locale")
{
    setup_test_locale();

    setup_test_locale();
    CHECK(from_string<bool>("True").value());
    setup_test_locale();
    CHECK(from_string<bool>("true").value());
    setup_test_locale();
    CHECK(from_string<bool>("1").value());

    setup_test_locale();
    CHECK(from_string<bool>("False").value() == false);
    setup_test_locale();
    CHECK(from_string<bool>("false").value() == false);
    setup_test_locale();
    CHECK(from_string<bool>("0").value() == false);
    setup_test_locale();

    setup_test_locale();
    CHECK(!from_string<bool>("vrai"));
    setup_test_locale();
    CHECK(!from_string<bool>("faux"));
    setup_test_locale();

    setup_test_locale();
    CHECK(from_string<bool>(to_string<bool>(true)).value());
    setup_test_locale();
    CHECK(from_string<bool>(to_string<bool>(false)).value() == false);
    setup_test_locale();
}

TEST_CASE("wf::int_wrapper_t locale")
{
    setup_test_locale();

    setup_test_locale();
    CHECK(from_string<int>("1456").value() == 1456);
    setup_test_locale();
    CHECK(from_string<int>("-89").value() == -89);

    int32_t max = std::numeric_limits<int32_t>::max();
    int32_t min = std::numeric_limits<int32_t>::min();
    setup_test_locale();
    CHECK(from_string<int>(wf::option_type::to_string(max)).value() == max);
    setup_test_locale();
    CHECK(from_string<int>(wf::option_type::to_string(min)).value() == min);

    setup_test_locale();
    CHECK(!from_string<int>("1e4"));
    setup_test_locale();
    CHECK(!from_string<int>(""));
    setup_test_locale();
    CHECK(!from_string<int>("1234567890000"));

    setup_test_locale();
    CHECK(from_string<int>(to_string<int>(456)).value() == 456);
    setup_test_locale();
    CHECK(from_string<int>(to_string<int>(0)).value() == 0);
}

TEST_CASE("wf::double_wrapper_t locale")
{
    setup_test_locale();
    CHECK(from_string<double>("0.378000").value() == doctest::Approx(0.378));
    setup_test_locale();
    CHECK(from_string<double>("-89.1847").value() == doctest::Approx(-89.1847));

    double max = std::numeric_limits<double>::max();
    double min = std::numeric_limits<double>::min();

    setup_test_locale();
    CHECK(from_string<double>(wf::option_type::to_string(
        max)).value() == doctest::Approx(max));
    setup_test_locale();
    CHECK(from_string<double>(wf::option_type::to_string(
        min)).value() == doctest::Approx(min));

    setup_test_locale();
    CHECK(!from_string<double>("1u4"));
    setup_test_locale();
    CHECK(!from_string<double>(""));
    setup_test_locale();
    CHECK(!from_string<double>("abc"));

    setup_test_locale();
    CHECK(from_string<double>(to_string<double>(-4.56)).value() ==
        doctest::Approx(-4.56));
    setup_test_locale();
    CHECK(from_string<double>(to_string<double>(0.0)).value() == doctest::Approx(0));
}

/* Test that various wf::color_t constructors work */
TEST_CASE("wf::color_t")
{
    using namespace wf;
    using namespace option_type;

    setup_test_locale();

    setup_test_locale();
    CHECK(!from_string<color_t>("#FFF"));
    setup_test_locale();
    CHECK(!from_string<color_t>("0C1A"));
    setup_test_locale();
    CHECK(!from_string<color_t>(""));
    setup_test_locale();
    CHECK(!from_string<color_t>("#ZYXUIOPQ"));
    setup_test_locale();
    CHECK(!from_string<color_t>("#AUIO")); // invalid color
    setup_test_locale();
    CHECK(!from_string<color_t>("1.0 0.5 0.5 1.0 1.0")); // invalid color
    setup_test_locale();
    CHECK(!from_string<color_t>("1.0 0.5 0.5 1.0 asdf")); // invalid color
    setup_test_locale();
    CHECK(!from_string<color_t>("1.0 0.5")); // invalid color

    setup_test_locale();
    CHECK(to_string<color_t>(color_t{0, 0, 0, 0}) == "#00000000");
    setup_test_locale();
    CHECK(to_string<color_t>(color_t{0.4, 0.8, 0.3686274,
        0.9686274}) == "#66CC5EF7");
    setup_test_locale();
    CHECK(to_string<color_t>(color_t{1, 1, 1, 1}) == "#FFFFFFFF");
}