summaryrefslogtreecommitdiffhomepage
path: root/test/app.cpp
blob: 6aaebb80f3a55998fbb2159541948609697523f1 (plain)
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
#include "app.hpp"
#include "loader/loader.hpp"
#include <stdlib.h> // NOLINT(*-deprecated-headers)
#include <cstdio>
#include <Corrade/Containers/StringView.h>
#include <Magnum/Math/Functions.h>
#include <Magnum/Timeline.h>
#include <Magnum/GL/Context.h>

namespace floormat {

namespace {

bool is_log_quiet() // copy-pasted from src/chunk.cpp
{
    using GLCCF = GL::Implementation::ContextConfigurationFlag;
    auto flags = GL::Context::current().configurationFlags();
    return !!(flags & GLCCF::QuietLog);
}

} // namespace

test_app::test_app(const Arguments& arguments):
      Application {
          arguments,
          Configuration{}
      }
{
}

test_app::~test_app()
{
    loader.destroy();
}

int test_app::exec()
{
    constexpr auto SV_flags = StringViewFlag::Global|StringViewFlag::NullTerminated;
    constexpr auto name_prefix = "test_"_s;

#define FM_TEST(name) { ( StringView{#name, arraySize(#name)-1, SV_flags} ), ( &(name) ), }

    constexpr struct {
        StringView name;
        void(*function)();
    } list[] = {
        FM_TEST(test_coords),
        FM_TEST(test_tile_iter),
        FM_TEST(test_magnum_math),
        FM_TEST(test_math),
        FM_TEST(test_entity),
        FM_TEST(test_intrusive_ptr),
        FM_TEST(test_hash),
        FM_TEST(test_json),
        FM_TEST(test_wall_atlas),
        FM_TEST(test_wall_atlas2),
        FM_TEST(test_bitmask),
        FM_TEST(test_loader),
        FM_TEST(test_serializer1),
        FM_TEST(test_scenery),
        FM_TEST(test_raycast),
        FM_TEST(test_path_search_pool),
        FM_TEST(test_path_search),
        FM_TEST(test_dijkstra), // todo add dummy atlases to avoid expensive loading
        FM_TEST(test_serializer2),
        FM_TEST(test_zzz_misc),
    };

    if (is_log_quiet())
        for (const auto [_, fun] : list)
            (*fun)();
    else
    {
        FILE* const s = stdout;
        static constexpr auto sep = ""_s;
        constexpr auto get_tabs = [](StringView name) constexpr {
            return (name.size()+sep.size()) / 8;
        };
        constexpr size_t tab_limit = 5;
        constexpr auto get_time = [](auto&& fn) {
            Timeline t;
            t.start();
            (*fn)();
            return t.currentFrameTime() * 1e3f;
        };

        size_t max_tabs = 1;
        for (const auto [name, _] : list)
            max_tabs = Math::max(max_tabs, get_tabs(name));
        max_tabs++;
        if (max_tabs > tab_limit)
            max_tabs = 1;

        std::fflush(s);

        for (auto [name, fun] : list)
        {
            name = name.exceptPrefix(name_prefix);
            std::fwrite(name.data(), name.size(), 1, s);
            std::fflush(stdout);
            auto ms = get_time(fun);
            std::fwrite(sep.data(), sep.size(), 1, s);
            auto num_tabs = max_tabs - get_tabs(name);
            fm_assert(num_tabs <= tab_limit);
            for (auto i = 0uz; i < num_tabs; i++)
                std::fputc('\t', s);
            std::fprintf(s, "% 9.3f ms\n", (double)ms);
            std::fflush(s);
        }
    }

    return 0;
}

} // namespace floormat

int main(int argc, char** argv)
{
#ifdef _WIN32
    // NOLINTNEXTLINE(concurrency-mt-unsafe)
    if (const auto* s = std::getenv("MAGNUM_LOG"); !s || !*s)
        ::_putenv("MAGNUM_LOG=quiet");
#else
        ::setenv("MAGNUM_LOG", "quiet", 0);
#endif
    floormat::test_app application{{argc, argv}};
    return application.exec();
}