test_quic_be_floats.c revision 06b2a236
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc.  See LICENSE. */
2#include <assert.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#ifndef WIN32
7#include <sys/time.h>
8#endif
9
10#include "lsquic.h"
11#include "lsquic_types.h"
12#include "lsquic_parse.h"
13
14
15static const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_043);
16
17struct float_test {
18    uint64_t    long_time;
19    uint8_t     float_time[2];
20};
21
22static const struct float_test to_float_tests[] = {
23    /* Small numbers represent themselves. */
24    { 0, { 0x00, 0x00, }, },
25    { 1, { 0x00, 0x01, }, },
26    { 2, { 0x00, 0x02, }, },
27    { 3, { 0x00, 0x03, }, },
28    { 4, { 0x00, 0x04, }, },
29    { 5, { 0x00, 0x05, }, },
30    { 6, { 0x00, 0x06, }, },
31    { 7, { 0x00, 0x07, }, },
32    { 15, { 0x00, 0x0F, }, },
33    { 31, { 0x00, 0x1F, }, },
34    { 42, { 0x00, 0x2A, }, },
35    { 123, { 0x00, 0x7B, }, },
36    { 1234, { 0x04, 0xD2, }, },
37    /*  Check transition through 2^11. */
38    { 2046, { 0x07, 0xFE, }, },
39    { 2047, { 0x07, 0xFF, }, },
40    { 2048, { 0x08, 0x00, }, },
41    { 2049, { 0x08, 0x01, }, },
42    /*  Running out of mantissa at 2^12. */
43    { 4094, { 0x0F, 0xFE, }, },
44    { 4095, { 0x0F, 0xFF, }, },
45    { 4096, { 0x10, 0x00, }, },
46    { 4097, { 0x10, 0x00, }, },
47    { 4098, { 0x10, 0x01, }, },
48    { 4099, { 0x10, 0x01, }, },
49    { 4100, { 0x10, 0x02, }, },
50    { 4101, { 0x10, 0x02, }, },
51    /*  Check transition through 2^13. */
52    { 8190, { 0x17, 0xFF, }, },
53    { 8191, { 0x17, 0xFF, }, },
54    { 8192, { 0x18, 0x00, }, },
55    { 8193, { 0x18, 0x00, }, },
56    { 8194, { 0x18, 0x00, }, },
57    { 8195, { 0x18, 0x00, }, },
58    { 8196, { 0x18, 0x01, }, },
59    { 8197, { 0x18, 0x01, }, },
60    /*  Half-way through the exponents. */
61    { 0x7FF8000, { 0x87, 0xFF, }, },
62    { 0x7FFFFFF, { 0x87, 0xFF, }, },
63    { 0x8000000, { 0x88, 0x00, }, },
64    { 0xFFF0000, { 0x8F, 0xFF, }, },
65    { 0xFFFFFFF, { 0x8F, 0xFF, }, },
66    { 0x10000000, { 0x90, 0x00, }, },
67    /*  Transition into the largest exponent. */
68    { 0x1FFFFFFFFFE, { 0xF7, 0xFF, }, },
69    { 0x1FFFFFFFFFF, { 0xF7, 0xFF, }, },
70    { 0x20000000000, { 0xF8, 0x00, }, },
71    { 0x20000000001, { 0xF8, 0x00, }, },
72    { 0x2003FFFFFFE, { 0xF8, 0x00, }, },
73    { 0x2003FFFFFFF, { 0xF8, 0x00, }, },
74    { 0x20040000000, { 0xF8, 0x01, }, },
75    { 0x20040000001, { 0xF8, 0x01, }, },
76    /*  Transition into the max value and clamping. */
77    { 0x3FF80000000, { 0xFF, 0xFE, }, },
78    { 0x3FFBFFFFFFF, { 0xFF, 0xFE, }, },
79    { 0x3FFC0000000, { 0xFF, 0xFF, }, },
80    { 0x3FFC0000001, { 0xFF, 0xFF, }, },
81    { 0x3FFFFFFFFFF, { 0xFF, 0xFF, }, },
82    { 0x40000000000, { 0xFF, 0xFF, }, },
83    { 0xFFFFFFFFFFFFFFFF, { 0xFF, 0xFF, }, },
84};
85
86
87static void
88run_to_float_tests (void)
89{
90    const struct float_test *test;
91    const struct float_test *const test_end =
92        &to_float_tests[ sizeof(to_float_tests) / sizeof(to_float_tests[0]) ];
93    for (test = to_float_tests; test < test_end; ++test)
94    {
95        char out[2];
96        pf->pf_write_float_time16(test->long_time, out);
97        assert(("Convertion to QUIC float format is successful",
98                                0 == memcmp(out, test->float_time, 2)));
99    }
100}
101
102
103static const struct float_test from_float_tests[] = {
104    /*  Small numbers represent themselves. */
105    { 0, { 0x00, 0x00, }, },
106    { 1, { 0x00, 0x01, }, },
107    { 2, { 0x00, 0x02, }, },
108    { 3, { 0x00, 0x03, }, },
109    { 4, { 0x00, 0x04, }, },
110    { 5, { 0x00, 0x05, }, },
111    { 6, { 0x00, 0x06, }, },
112    { 7, { 0x00, 0x07, }, },
113    { 15, { 0x00, 0x0F, }, },
114    { 31, { 0x00, 0x1F, }, },
115    { 42, { 0x00, 0x2A, }, },
116    { 123, { 0x00, 0x7B, }, },
117    { 1234, { 0x04, 0xD2, }, },
118    /*  Check transition through 2^11. */
119    { 2046, { 0x07, 0xFE, }, },
120    { 2047, { 0x07, 0xFF, }, },
121    { 2048, { 0x08, 0x00, }, },
122    { 2049, { 0x08, 0x01, }, },
123    /*  Running out of mantissa at 2^12. */
124    { 4094, { 0x0F, 0xFE, }, },
125    { 4095, { 0x0F, 0xFF, }, },
126    { 4096, { 0x10, 0x00, }, },
127    { 4098, { 0x10, 0x01, }, },
128    { 4100, { 0x10, 0x02, }, },
129    /*  Check transition through 2^13. */
130    { 8190, { 0x17, 0xFF, }, },
131    { 8192, { 0x18, 0x00, }, },
132    { 8196, { 0x18, 0x01, }, },
133    /*  Half-way through the exponents. */
134    { 0x7FF8000, { 0x87, 0xFF, }, },
135    { 0x8000000, { 0x88, 0x00, }, },
136    { 0xFFF0000, { 0x8F, 0xFF, }, },
137    { 0x10000000, { 0x90, 0x00, }, },
138    /*  Transition into the largest exponent. */
139    { 0x1FFE0000000, { 0xF7, 0xFF, }, },
140    { 0x20000000000, { 0xF8, 0x00, }, },
141    { 0x20040000000, { 0xF8, 0x01, }, },
142    /*  Transition into the max value. */
143    { 0x3FF80000000, { 0xFF, 0xFE, }, },
144    { 0x3FFC0000000, { 0xFF, 0xFF, }, },
145};
146
147
148static void
149run_from_float_tests (void)
150{
151    const struct float_test *test;
152    const struct float_test *const test_end =
153        &from_float_tests[ sizeof(from_float_tests) / sizeof(from_float_tests[0]) ];
154    for (test = from_float_tests; test < test_end; ++test)
155    {
156        uint64_t result = pf->pf_read_float_time16(test->float_time);
157        assert(("Convertion to QUIC float format is successful",
158                                                result == test->long_time));
159    }
160}
161
162
163int
164main (void)
165{
166    run_to_float_tests();
167    run_from_float_tests();
168    return 0;
169}
170