1229fce07SDmitri Tikhonov/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <assert.h>
350aadb33SDmitri Tikhonov#include <stdio.h>
450aadb33SDmitri Tikhonov#include <stdlib.h>
550aadb33SDmitri Tikhonov#include <string.h>
6461e84d8SAmol Deshpande#ifndef WIN32
750aadb33SDmitri Tikhonov#include <sys/time.h>
8461e84d8SAmol Deshpande#endif
950aadb33SDmitri Tikhonov
1050aadb33SDmitri Tikhonov#include "lsquic.h"
1150aadb33SDmitri Tikhonov#include "lsquic_types.h"
1250aadb33SDmitri Tikhonov#include "lsquic_alarmset.h"
1350aadb33SDmitri Tikhonov#include "lsquic_parse.h"
1450aadb33SDmitri Tikhonov
1550aadb33SDmitri Tikhonov
16052a1c28SDmitri Tikhonovstatic const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_035);
1750aadb33SDmitri Tikhonov
1850aadb33SDmitri Tikhonovstruct float_test {
1950aadb33SDmitri Tikhonov    uint64_t    long_time;
2050aadb33SDmitri Tikhonov    uint8_t     float_time[2];
2150aadb33SDmitri Tikhonov};
2250aadb33SDmitri Tikhonov
2350aadb33SDmitri Tikhonovstatic const struct float_test to_float_tests[] = {
2450aadb33SDmitri Tikhonov    /* Small numbers represent themselves. */
2550aadb33SDmitri Tikhonov    { 0, { 0x00, 0x00, }, },
2650aadb33SDmitri Tikhonov    { 1, { 0x01, 0x00, }, },
2750aadb33SDmitri Tikhonov    { 2, { 0x02, 0x00, }, },
2850aadb33SDmitri Tikhonov    { 3, { 0x03, 0x00, }, },
2950aadb33SDmitri Tikhonov    { 4, { 0x04, 0x00, }, },
3050aadb33SDmitri Tikhonov    { 5, { 0x05, 0x00, }, },
3150aadb33SDmitri Tikhonov    { 6, { 0x06, 0x00, }, },
3250aadb33SDmitri Tikhonov    { 7, { 0x07, 0x00, }, },
3350aadb33SDmitri Tikhonov    { 15, { 0x0F, 0x00, }, },
3450aadb33SDmitri Tikhonov    { 31, { 0x1F, 0x00, }, },
3550aadb33SDmitri Tikhonov    { 42, { 0x2A, 0x00, }, },
3650aadb33SDmitri Tikhonov    { 123, { 0x7B, 0x00, }, },
3750aadb33SDmitri Tikhonov    { 1234, { 0xD2, 0x04, }, },
3850aadb33SDmitri Tikhonov    /*  Check transition through 2^11. */
3950aadb33SDmitri Tikhonov    { 2046, { 0xFE, 0x07, }, },
4050aadb33SDmitri Tikhonov    { 2047, { 0xFF, 0x07, }, },
4150aadb33SDmitri Tikhonov    { 2048, { 0x00, 0x08, }, },
4250aadb33SDmitri Tikhonov    { 2049, { 0x01, 0x08, }, },
4350aadb33SDmitri Tikhonov    /*  Running out of mantissa at 2^12. */
4450aadb33SDmitri Tikhonov    { 4094, { 0xFE, 0x0F, }, },
4550aadb33SDmitri Tikhonov    { 4095, { 0xFF, 0x0F, }, },
4650aadb33SDmitri Tikhonov    { 4096, { 0x00, 0x10, }, },
4750aadb33SDmitri Tikhonov    { 4097, { 0x00, 0x10, }, },
4850aadb33SDmitri Tikhonov    { 4098, { 0x01, 0x10, }, },
4950aadb33SDmitri Tikhonov    { 4099, { 0x01, 0x10, }, },
5050aadb33SDmitri Tikhonov    { 4100, { 0x02, 0x10, }, },
5150aadb33SDmitri Tikhonov    { 4101, { 0x02, 0x10, }, },
5250aadb33SDmitri Tikhonov    /*  Check transition through 2^13. */
5350aadb33SDmitri Tikhonov    { 8190, { 0xFF, 0x17, }, },
5450aadb33SDmitri Tikhonov    { 8191, { 0xFF, 0x17, }, },
5550aadb33SDmitri Tikhonov    { 8192, { 0x00, 0x18, }, },
5650aadb33SDmitri Tikhonov    { 8193, { 0x00, 0x18, }, },
5750aadb33SDmitri Tikhonov    { 8194, { 0x00, 0x18, }, },
5850aadb33SDmitri Tikhonov    { 8195, { 0x00, 0x18, }, },
5950aadb33SDmitri Tikhonov    { 8196, { 0x01, 0x18, }, },
6050aadb33SDmitri Tikhonov    { 8197, { 0x01, 0x18, }, },
6150aadb33SDmitri Tikhonov    /*  Half-way through the exponents. */
6250aadb33SDmitri Tikhonov    { 0x7FF8000, { 0xFF, 0x87, }, },
6350aadb33SDmitri Tikhonov    { 0x7FFFFFF, { 0xFF, 0x87, }, },
6450aadb33SDmitri Tikhonov    { 0x8000000, { 0x00, 0x88, }, },
6550aadb33SDmitri Tikhonov    { 0xFFF0000, { 0xFF, 0x8F, }, },
6650aadb33SDmitri Tikhonov    { 0xFFFFFFF, { 0xFF, 0x8F, }, },
6750aadb33SDmitri Tikhonov    { 0x10000000, { 0x00, 0x90, }, },
6850aadb33SDmitri Tikhonov    /*  Transition into the largest exponent. */
6950aadb33SDmitri Tikhonov    { 0x1FFFFFFFFFE, { 0xFF, 0xF7, }, },
7050aadb33SDmitri Tikhonov    { 0x1FFFFFFFFFF, { 0xFF, 0xF7, }, },
7150aadb33SDmitri Tikhonov    { 0x20000000000, { 0x00, 0xF8, }, },
7250aadb33SDmitri Tikhonov    { 0x20000000001, { 0x00, 0xF8, }, },
7350aadb33SDmitri Tikhonov    { 0x2003FFFFFFE, { 0x00, 0xF8, }, },
7450aadb33SDmitri Tikhonov    { 0x2003FFFFFFF, { 0x00, 0xF8, }, },
7550aadb33SDmitri Tikhonov    { 0x20040000000, { 0x01, 0xF8, }, },
7650aadb33SDmitri Tikhonov    { 0x20040000001, { 0x01, 0xF8, }, },
7750aadb33SDmitri Tikhonov    /*  Transition into the max value and clamping. */
7850aadb33SDmitri Tikhonov    { 0x3FF80000000, { 0xFE, 0xFF, }, },
7950aadb33SDmitri Tikhonov    { 0x3FFBFFFFFFF, { 0xFE, 0xFF, }, },
8050aadb33SDmitri Tikhonov    { 0x3FFC0000000, { 0xFF, 0xFF, }, },
8150aadb33SDmitri Tikhonov    { 0x3FFC0000001, { 0xFF, 0xFF, }, },
8250aadb33SDmitri Tikhonov    { 0x3FFFFFFFFFF, { 0xFF, 0xFF, }, },
8350aadb33SDmitri Tikhonov    { 0x40000000000, { 0xFF, 0xFF, }, },
8450aadb33SDmitri Tikhonov    { 0xFFFFFFFFFFFFFFFF, { 0xFF, 0xFF, }, },
8550aadb33SDmitri Tikhonov};
8650aadb33SDmitri Tikhonov
8750aadb33SDmitri Tikhonov
8850aadb33SDmitri Tikhonovstatic void
8950aadb33SDmitri Tikhonovrun_to_float_tests (void)
9050aadb33SDmitri Tikhonov{
9150aadb33SDmitri Tikhonov    const struct float_test *test;
9250aadb33SDmitri Tikhonov    const struct float_test *const test_end =
9350aadb33SDmitri Tikhonov        &to_float_tests[ sizeof(to_float_tests) / sizeof(to_float_tests[0]) ];
9450aadb33SDmitri Tikhonov    for (test = to_float_tests; test < test_end; ++test)
9550aadb33SDmitri Tikhonov    {
9650aadb33SDmitri Tikhonov        char out[2];
9750aadb33SDmitri Tikhonov        pf->pf_write_float_time16(test->long_time, out);
9850aadb33SDmitri Tikhonov        assert(("Convertion to QUIC float format is successful",
9950aadb33SDmitri Tikhonov                                0 == memcmp(out, test->float_time, 2)));
10050aadb33SDmitri Tikhonov    }
10150aadb33SDmitri Tikhonov}
10250aadb33SDmitri Tikhonov
10350aadb33SDmitri Tikhonov
10450aadb33SDmitri Tikhonovstatic const struct float_test from_float_tests[] = {
10550aadb33SDmitri Tikhonov    /*  Small numbers represent themselves. */
10650aadb33SDmitri Tikhonov    { 0, { 0x00, 0x00, }, },
10750aadb33SDmitri Tikhonov    { 1, { 0x01, 0x00, }, },
10850aadb33SDmitri Tikhonov    { 2, { 0x02, 0x00, }, },
10950aadb33SDmitri Tikhonov    { 3, { 0x03, 0x00, }, },
11050aadb33SDmitri Tikhonov    { 4, { 0x04, 0x00, }, },
11150aadb33SDmitri Tikhonov    { 5, { 0x05, 0x00, }, },
11250aadb33SDmitri Tikhonov    { 6, { 0x06, 0x00, }, },
11350aadb33SDmitri Tikhonov    { 7, { 0x07, 0x00, }, },
11450aadb33SDmitri Tikhonov    { 15, { 0x0F, 0x00, }, },
11550aadb33SDmitri Tikhonov    { 31, { 0x1F, 0x00, }, },
11650aadb33SDmitri Tikhonov    { 42, { 0x2A, 0x00, }, },
11750aadb33SDmitri Tikhonov    { 123, { 0x7B, 0x00, }, },
11850aadb33SDmitri Tikhonov    { 1234, { 0xD2, 0x04, }, },
11950aadb33SDmitri Tikhonov    /*  Check transition through 2^11. */
12050aadb33SDmitri Tikhonov    { 2046, { 0xFE, 0x07, }, },
12150aadb33SDmitri Tikhonov    { 2047, { 0xFF, 0x07, }, },
12250aadb33SDmitri Tikhonov    { 2048, { 0x00, 0x08, }, },
12350aadb33SDmitri Tikhonov    { 2049, { 0x01, 0x08, }, },
12450aadb33SDmitri Tikhonov    /*  Running out of mantissa at 2^12. */
12550aadb33SDmitri Tikhonov    { 4094, { 0xFE, 0x0F, }, },
12650aadb33SDmitri Tikhonov    { 4095, { 0xFF, 0x0F, }, },
12750aadb33SDmitri Tikhonov    { 4096, { 0x00, 0x10, }, },
12850aadb33SDmitri Tikhonov    { 4098, { 0x01, 0x10, }, },
12950aadb33SDmitri Tikhonov    { 4100, { 0x02, 0x10, }, },
13050aadb33SDmitri Tikhonov    /*  Check transition through 2^13. */
13150aadb33SDmitri Tikhonov    { 8190, { 0xFF, 0x17, }, },
13250aadb33SDmitri Tikhonov    { 8192, { 0x00, 0x18, }, },
13350aadb33SDmitri Tikhonov    { 8196, { 0x01, 0x18, }, },
13450aadb33SDmitri Tikhonov    /*  Half-way through the exponents. */
13550aadb33SDmitri Tikhonov    { 0x7FF8000, { 0xFF, 0x87, }, },
13650aadb33SDmitri Tikhonov    { 0x8000000, { 0x00, 0x88, }, },
13750aadb33SDmitri Tikhonov    { 0xFFF0000, { 0xFF, 0x8F, }, },
13850aadb33SDmitri Tikhonov    { 0x10000000, { 0x00, 0x90, }, },
13950aadb33SDmitri Tikhonov    /*  Transition into the largest exponent. */
14050aadb33SDmitri Tikhonov    { 0x1FFE0000000, { 0xFF, 0xF7, }, },
14150aadb33SDmitri Tikhonov    { 0x20000000000, { 0x00, 0xF8, }, },
14250aadb33SDmitri Tikhonov    { 0x20040000000, { 0x01, 0xF8, }, },
14350aadb33SDmitri Tikhonov    /*  Transition into the max value. */
14450aadb33SDmitri Tikhonov    { 0x3FF80000000, { 0xFE, 0xFF, }, },
14550aadb33SDmitri Tikhonov    { 0x3FFC0000000, { 0xFF, 0xFF, }, },
14650aadb33SDmitri Tikhonov};
14750aadb33SDmitri Tikhonov
14850aadb33SDmitri Tikhonov
14950aadb33SDmitri Tikhonovstatic void
15050aadb33SDmitri Tikhonovrun_from_float_tests (void)
15150aadb33SDmitri Tikhonov{
15250aadb33SDmitri Tikhonov    const struct float_test *test;
15350aadb33SDmitri Tikhonov    const struct float_test *const test_end =
15450aadb33SDmitri Tikhonov        &from_float_tests[ sizeof(from_float_tests) / sizeof(from_float_tests[0]) ];
15550aadb33SDmitri Tikhonov    for (test = from_float_tests; test < test_end; ++test)
15650aadb33SDmitri Tikhonov    {
15750aadb33SDmitri Tikhonov        uint64_t result = pf->pf_read_float_time16(test->float_time);
15850aadb33SDmitri Tikhonov        assert(("Convertion to QUIC float format is successful",
15950aadb33SDmitri Tikhonov                                                result == test->long_time));
16050aadb33SDmitri Tikhonov    }
16150aadb33SDmitri Tikhonov}
16250aadb33SDmitri Tikhonov
16350aadb33SDmitri Tikhonov
16450aadb33SDmitri Tikhonovint
16550aadb33SDmitri Tikhonovmain (void)
16650aadb33SDmitri Tikhonov{
16750aadb33SDmitri Tikhonov    run_to_float_tests();
16850aadb33SDmitri Tikhonov    run_from_float_tests();
16950aadb33SDmitri Tikhonov    return 0;
17050aadb33SDmitri Tikhonov}
171