summaryrefslogtreecommitdiffstats
path: root/tools/slang-test/unit-test-string.cpp
blob: e8a33fbbe7d9685efd60a54507e4878ad96fc9f9 (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
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
// unit-test-path.cpp

#include "../../source/core/slang-string-util.h"

#include "test-context.h"

//#include <math.h>

#include <sstream>

using namespace Slang;

static bool _areEqual(const List<UnownedStringSlice>& lines, const UnownedStringSlice* checkLines, Int checkLinesCount)
{
    if (checkLinesCount != lines.getCount())
    {
        return false;
    }

    for (Int i = 0; i < checkLinesCount; ++i)
    {
        if (lines[i] != checkLines[i])
        {
            return false;
        }
    }
    return true;
}

static bool _checkLines(const UnownedStringSlice& input, const UnownedStringSlice* checkLines, Int checkLinesCount)
{
    List<UnownedStringSlice> lines;
    StringUtil::calcLines(input, lines);
    return _areEqual(lines, checkLines, checkLinesCount);
}

static bool _checkLineParser(const UnownedStringSlice& input)
{
    UnownedStringSlice remaining(input), line;
    for (const auto parserLine : LineParser(input))
    {
        if (!StringUtil::extractLine(remaining, line) || line != parserLine)
        {
            return false;
        }
    }
    return StringUtil::extractLine(remaining, line) == false;
}

static void _append(double v, StringBuilder& buf)
{
    std::ostringstream stream;
    stream.imbue(std::locale::classic());
    stream.setf(std::ios::fixed, std::ios::floatfield);
    stream.precision(20);

    stream << std::scientific << v;

    buf << stream.str().c_str();
}

// Unit of least precision
static int64_t _calcULPDistance(double a, double b)
{
    // Save work if the floats are equal.
    // Also handles +0 == -0
    if (a == b)
    {
        return 0;
    }

    const int64_t max = int64_t((~uint64_t(0)) >> 1);

#if 0
    // Max distance for NaN
    if (isnan(a) || isnan(b))
    {
        return max;
    }

    // If one's infinite and they're not equal, max distance.
    if (isinf(a) || isinf(b))
    {
        return max;
    }
#endif

    int64_t ia, ib;
    memcpy(&ia, &a, sizeof(a));
    memcpy(&ib, &b, sizeof(b));

    // Don't compare differently-signed floats.
    if ((ia < 0) != (ib < 0))
    {
        return max;
    }

    // Return the absolute value of the distance in ULPs.
    int64_t distance = ia - ib;
    return distance < 0 ? -distance : distance;
}

static bool _areApproximatelyEqual(double a, double b, double fixedEpsilon = 1e-10, int ulpsEpsilon = 100)
{
    // Handle the near-zero case.
    const double difference = abs(a - b);
    if (difference <= fixedEpsilon)
    {
        return true;
    }

    return _calcULPDistance(a, b) <= ulpsEpsilon;
}

static void stringUnitTest()
{
    {
        UnownedStringSlice checkLines[] = { UnownedStringSlice::fromLiteral("") };
        SLANG_CHECK(_checkLines(UnownedStringSlice::fromLiteral(""), checkLines, SLANG_COUNT_OF(checkLines)));
    }
    {
        // Will emit no lines
        SLANG_CHECK(_checkLines(UnownedStringSlice(nullptr, nullptr), nullptr, 0));
    }
    {
        // Two lines - both empty
        UnownedStringSlice checkLines[] = { UnownedStringSlice(), UnownedStringSlice()};
        SLANG_CHECK(_checkLines(UnownedStringSlice::fromLiteral("\n"), checkLines, SLANG_COUNT_OF(checkLines)));
    }
    {
        UnownedStringSlice checkLines[] = { UnownedStringSlice::fromLiteral("Hello"), UnownedStringSlice::fromLiteral("World!") };
        SLANG_CHECK(_checkLines(UnownedStringSlice::fromLiteral("Hello\nWorld!"), checkLines, SLANG_COUNT_OF(checkLines)));
    }
    {
        UnownedStringSlice checkLines[] = { UnownedStringSlice::fromLiteral("Hello"), UnownedStringSlice::fromLiteral("World!"), UnownedStringSlice() };
        SLANG_CHECK(_checkLines(UnownedStringSlice::fromLiteral("Hello\n\rWorld!\n"), checkLines, SLANG_COUNT_OF(checkLines)));
    }

    {
        SLANG_CHECK(_checkLineParser(UnownedStringSlice::fromLiteral("Hello\n\rWorld!\n")));
        SLANG_CHECK(_checkLineParser(UnownedStringSlice::fromLiteral("\n")));
        SLANG_CHECK(_checkLineParser(UnownedStringSlice::fromLiteral("")));
    }
    {
        Int value;
        SLANG_CHECK(SLANG_SUCCEEDED(StringUtil::parseInt(UnownedStringSlice("-10"), value)) && value == -10);
        SLANG_CHECK(SLANG_SUCCEEDED(StringUtil::parseInt(UnownedStringSlice("0"), value)) && value == 0);
        SLANG_CHECK(SLANG_SUCCEEDED(StringUtil::parseInt(UnownedStringSlice("-0"), value)) && value == 0);

        SLANG_CHECK(SLANG_SUCCEEDED(StringUtil::parseInt(UnownedStringSlice("13824"), value)) && value == 13824);
        SLANG_CHECK(SLANG_SUCCEEDED(StringUtil::parseInt(UnownedStringSlice("-13824"), value)) && value == -13824);
    }

    {
        UnownedStringSlice values[] = { UnownedStringSlice("hello"), UnownedStringSlice("world"), UnownedStringSlice("!") };
        ArrayView<UnownedStringSlice> valuesView(values, SLANG_COUNT_OF(values));

        List<UnownedStringSlice> checkValues;
        StringBuilder builder;

        {
            builder.Clear();
            StringUtil::join(values, 0, ',', builder);
            SLANG_CHECK(builder == "");
        }

        {
            builder.Clear();
            StringUtil::join(values, 1, ',', builder);
            SLANG_CHECK(builder == "hello");

            StringUtil::split(builder.getUnownedSlice(), ',', checkValues);
            SLANG_CHECK(checkValues.getArrayView() == ArrayView<UnownedStringSlice>(values, 1));
        }

        {
            builder.Clear();
            StringUtil::join(values, 2, ',', builder);
            SLANG_CHECK(builder == "hello,world");

            StringUtil::split(builder.getUnownedSlice(), ',', checkValues);
            SLANG_CHECK(checkValues.getArrayView() == ArrayView<UnownedStringSlice>(values, 2));
        }

        {
            builder.Clear();
            StringUtil::join(values, 3, UnownedStringSlice("ab"), builder);
            SLANG_CHECK(builder == "helloabworldab!");

            StringUtil::split(builder.getUnownedSlice(), UnownedStringSlice("ab"), checkValues);
            SLANG_CHECK(checkValues.getArrayView() == ArrayView<UnownedStringSlice>(values, 3));
        }
    }
    {
       
        List<double> values;
        values.add(0.0);
        values.add(-0.0);

        for (Index i = -300; i < 300; ++i)
        {
            double value = pow(10, i);

            values.add(value);
            values.add(-value);

            values.addRange(value / 3);
            values.addRange(-value / 3);
        }

        StringBuilder buf;

        for (auto value : values)
        {
            buf.Clear();
            _append(value, buf);

            UnownedStringSlice slice = buf.getUnownedSlice();

            double parsedValue;
            SlangResult res = StringUtil::parseDouble(slice, parsedValue);

            auto ulpsParsed = _calcULPDistance(value, parsedValue);
            
            SLANG_CHECK(SLANG_SUCCEEDED(res));

            // Check that they are equal
            SLANG_CHECK(_areApproximatelyEqual(value, parsedValue));
        }
    }
    {
        List<int64_t> values;
        values.add(0);

        for (Index i = 0; i < 63; ++i)
        {
            auto value = int64_t(1) << i;

            values.add(value);
            values.add(-value);
        }

        StringBuilder buf;

        for (auto value : values)
        {
            buf.Clear();
            buf << value;


            int64_t parsedValue;

            UnownedStringSlice slice = buf.getUnownedSlice();
            SlangResult res = StringUtil::parseInt64(slice, parsedValue);

            SLANG_CHECK(SLANG_SUCCEEDED(res));
            
            // Check that they are equal
            SLANG_CHECK(value == parsedValue);
        }
    }
}

SLANG_UNIT_TEST("String", stringUnitTest);