constrained.tests.cpp

 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
#include <planet/ui/constrained.hpp>
#include <felspar/test.hpp>


namespace {


    auto const c1d = felspar::testsuite("constrained1d");


    auto const n1d = c1d.test("comparisons", [](auto check) {
        check(planet::ui::constrained1d{42}) == 42;
        check(42) == planet::ui::constrained1d{42};
        check(planet::ui::constrained1d{42}) == planet::ui::constrained1d{42};

        check(planet::ui::constrained1d{42}) != 43;
        check(42) != planet::ui::constrained1d{43};
        check(planet::ui::constrained1d{42}) != planet::ui::constrained1d{43};

        check(planet::ui::constrained1d{42}) != 43L;
        check(42) != planet::ui::constrained1d{43L};
        check(planet::ui::constrained1d{42}) != planet::ui::constrained1d{43L};

        check(planet::ui::constrained1d{42, 0, 100}
                      .is_at_least_as_constrained_as(
                              planet::ui::constrained1d{54, 0, 100}))
                == true;
        check(planet::ui::constrained1d{42, 10, 100}
                      .is_at_least_as_constrained_as(
                              planet::ui::constrained1d{54, 0, 100}))
                == true;
        check(planet::ui::constrained1d{42, 0, 90}.is_at_least_as_constrained_as(
                planet::ui::constrained1d{54, 0, 100}))
                == true;
        check(planet::ui::constrained1d{42, 0, 100}
                      .is_at_least_as_constrained_as(
                              planet::ui::constrained1d{54, 10, 100}))
                == false;
        check(planet::ui::constrained1d{42, 0, 100}
                      .is_at_least_as_constrained_as(
                              planet::ui::constrained1d{54, 0, 90}))
                == false;
    });


    auto const d1d = c1d.test("desired", [](auto check) {
        planet::ui::constrained1d v1{42, {}, 100};
        check(v1) == 42;

        v1.min(50);
        check(v1) == 50;
        v1.min(0);
        check(v1) == 42;

        v1.max(20);
        check(v1) == 20;
        v1.max(42);
        check(v1) == 42;
    });


    auto const l1d = c1d.test("normalisation", [](auto check) {
        planet::ui::constrained1d<float> db{-6, -128, 0};
        planet::ui::constrained1d<float> pixels{300, 200, 456};

        check(db.normalised_value()) == 122.0f / 128.0f;
        check(db.remapped_to(pixels)) == 444;

        check(pixels.normalised_value()) == 100.0f / 256.0f;
        check(pixels.remapped_to(db)) == -78.0f;
    });


    auto const c2d = felspar::testsuite("constrained2d");


    auto const n2d = c2d.test("comparisons", [](auto check) {
        check(planet::ui::constrained2d{3, 5})
                == planet::ui::constrained2d{3, 5};
    });


    auto const con2d = c2d.test("construction", [](auto check) {
        auto const c = planet::ui::constrained2d<float>{{4, 2, 10}, {6, 1, 8}};

        check(c.min_extents()) == planet::affine::extents2d{2, 1};
        check(c.extents()) == planet::affine::extents2d{4, 6};
        check(c.max_extents()) == planet::affine::extents2d{10, 8};
    });


}