Skip to content
This repository
tree: 363e5ecfce
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 95 lines (81 sloc) 3.042 kb
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
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "switch/NumberCompress.h"

#include "util/Assert.h"
#include <stdio.h>
#include <inttypes.h>

static void numberCompressions_generic(
    uint32_t nInterfaces,
    uint32_t (*bitsUsedForLabel)(const uint64_t label),
    uint32_t (*bitsUsedForNumber)(const uint32_t number),
    uint64_t (*getCompressed)(const uint32_t number, const uint32_t bitsUsed),
    uint32_t (*getDecompressed)(const uint64_t label, const uint32_t bitsUsed))
{

    uint8_t bitWidths[64] = { 0 };

    for (uint32_t i = 0; i < nInterfaces; ++i) {
        bitWidths[bitsUsedForNumber(i)] = 1;
    }

    for (uint32_t bits = 0; bits < 64; ++bits) {
        if (!bitWidths[bits]) {
            continue;
        }

        for (uint32_t i = 0; i < nInterfaces; ++i) {
            /* only check for greater-or-equal bit widths */
            if (bits < bitsUsedForNumber(i)) {
                continue;
            }

            uint64_t label = getCompressed(i, bits);

            if (1 == i) {
                Assert_always(1 == label);
                continue;
            }

            Assert_always(bits == bitsUsedForLabel(label));
            Assert_always(i == getDecompressed(label, bits));
        }
    }

    for (uint64_t label = 0; label < 0x10000u; ++label) {
        uint32_t bits = bitsUsedForLabel(label);
        Assert_always(1 == bitWidths[bits]);
        if (1 == (label & 0xf)) {
            Assert_always(4 == bits);
            Assert_always(1 == getDecompressed(label, 4));
        } else {
            uint32_t i = getDecompressed(label, bits);
            Assert_always(i < nInterfaces);
        }
    }
}

int main()
{
    numberCompressions_generic(
        NumberCompress_4_INTERFACES,
        NumberCompress_4_bitsUsedForLabel,
        NumberCompress_4_bitsUsedForNumber,
        NumberCompress_4_getCompressed,
        NumberCompress_4_getDecompressed);

    numberCompressions_generic(
        NumberCompress_8_INTERFACES,
        NumberCompress_8_bitsUsedForLabel,
        NumberCompress_8_bitsUsedForNumber,
        NumberCompress_8_getCompressed,
        NumberCompress_8_getDecompressed);

    numberCompressions_generic(
        NumberCompress_dyn_INTERFACES,
        NumberCompress_dyn_bitsUsedForLabel,
        NumberCompress_dyn_bitsUsedForNumber,
        NumberCompress_dyn_getCompressed,
        NumberCompress_dyn_getDecompressed);
    return 0;
}
Something went wrong with that request. Please try again.