summaryrefslogtreecommitdiff
path: root/asl/tests/format_tests.cpp
blob: f4ca2cfbd3b1037bf68313f66bd3aa32b35fd905 (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
#include "asl/format.hpp"
#include "asl/testing/testing.hpp"

#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cstdio>

// @Todo Improve this to use our utilities, not the C stdlib

static_assert(asl::formattable<decltype("Hello")>);

class StringSink : public asl::writer
{
    int64_t m_current_len{};
    char*   m_data{};
    
public:
    void write(const char* str, int64_t len) override
    {
        m_data = (char*)realloc(m_data, (size_t)(m_current_len + len + 1));
        memcpy(m_data + m_current_len, str, (size_t)len);
        m_current_len += len;
        m_data[m_current_len] = '\0';
    }

    constexpr const char* cstr() const { return m_data; }

    void reset()
    {
        m_current_len = 0;
        free(m_data);
        m_data = nullptr;
    }
};

ASL_TEST(Format)
{
    StringSink sink;

    // @Todo Introduce ASL_TEST_ASSERT_EQ, or ASL_TEST_ASSERT_STREQ
    // @Todo Don't use strcmp for string comparison

    asl::format(&sink, "Hello, world!");
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "Hello, world!") == 0);

    sink.reset();
    asl::format(&sink, "");
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "") == 0);

    sink.reset();
    asl::format(&sink, "Hello, {}!", "world");
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "Hello, world!") == 0);

    sink.reset();
    asl::format(&sink, "Hello, {}! {}", "world");
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "Hello, world! <ERROR>") == 0);

    sink.reset();
    asl::format(&sink, "Hello, pup!", "world");
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "Hello, pup!") == 0);

    sink.reset();
    asl::format(&sink, "{}", "CHEESE");
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "CHEESE") == 0);

    sink.reset();
    asl::format(&sink, "{   ", "CHEESE");
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "<ERROR>   ") == 0);

    sink.reset();
    asl::format(&sink, "{", "CHEESE");
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "<ERROR>") == 0);

    sink.reset();
    asl::format(&sink, "a{{b");
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "a{b") == 0);

    sink.reset();
    asl::format(&sink, "{{{}}} }", "CHEESE");
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "{CHEESE} }") == 0);

    sink.reset();
    asl::format(&sink, "{} {} {}", 0, 1, 2);
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "0 1 2") == 0);

    sink.reset();
    asl::format(&sink, "{} {} {}", 10, 11, 12);
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "10 11 12") == 0);

    sink.reset();
    asl::format(&sink, "{} {} {}", 100, 101, 102);
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "100 101 102") == 0);

    sink.reset();
    asl::format(&sink, "{} {} {}", 1000, 1001, 1002);
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "1000 1001 1002") == 0);

    sink.reset();
    asl::format(&sink, "{} {} {} {}", -1, -23, -456, -7890);
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "-1 -23 -456 -7890") == 0);

    sink.reset();
    asl::format(&sink, "{} {}", true, false);
    ASL_TEST_ASSERT(strcmp(sink.cstr(), "true false") == 0);
}