File: test-bitset.c

package info (click to toggle)
libcork 0.15.0%2Bds-16
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,140 kB
  • sloc: ansic: 12,216; python: 206; sh: 126; makefile: 16
file content (103 lines) | stat: -rw-r--r-- 2,416 bytes parent folder | download | duplicates (2)
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
/* -*- coding: utf-8 -*-
 * ----------------------------------------------------------------------
 * Copyright © 2013-2014, RedJack, LLC.
 * All rights reserved.
 *
 * Please see the COPYING file in this distribution for license details.
 * ----------------------------------------------------------------------
 */

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>

#include <check.h>

#include "libcork/core/types.h"
#include "libcork/ds/bitset.h"

#include "helpers.h"


/*-----------------------------------------------------------------------
 * Bit sets
 */

static void
test_bitset_of_size(size_t bit_count)
{
    size_t  i;
    struct cork_bitset  *set = cork_bitset_new(bit_count);

    for (i = 0; i < bit_count; i++) {
        cork_bitset_set(set, i, true);
        fail_unless(cork_bitset_get(set, i), "Unexpected value for bit %zu", i);
    }

    for (i = 0; i < bit_count; i++) {
        cork_bitset_set(set, i, false);
        fail_if(cork_bitset_get(set, i), "Unexpected value for bit %zu", i);
    }

    cork_bitset_free(set);
}

START_TEST(test_bitset)
{
    DESCRIBE_TEST;
    /* Test a variety of sizes, with and without spillover bits. */
    test_bitset_of_size(1);
    test_bitset_of_size(2);
    test_bitset_of_size(3);
    test_bitset_of_size(4);
    test_bitset_of_size(5);
    test_bitset_of_size(6);
    test_bitset_of_size(7);
    test_bitset_of_size(8);
    test_bitset_of_size(9);
    test_bitset_of_size(10);
    test_bitset_of_size(11);
    test_bitset_of_size(12);
    test_bitset_of_size(13);
    test_bitset_of_size(14);
    test_bitset_of_size(15);
    test_bitset_of_size(16);
    test_bitset_of_size(65535);
    test_bitset_of_size(65536);
    test_bitset_of_size(65537);
}
END_TEST


/*-----------------------------------------------------------------------
 * Testing harness
 */

Suite *
test_suite()
{
    Suite  *s = suite_create("bits");

    TCase  *tc_ds = tcase_create("bits");
    tcase_set_timeout(tc_ds, 120.0);
    tcase_add_test(tc_ds, test_bitset);
    suite_add_tcase(s, tc_ds);

    return s;
}


int
main(int argc, const char **argv)
{
    int  number_failed;
    Suite  *suite = test_suite();
    SRunner  *runner = srunner_create(suite);

    setup_allocator();
    srunner_run_all(runner, CK_NORMAL);
    number_failed = srunner_ntests_failed(runner);
    srunner_free(runner);

    return (number_failed == 0)? EXIT_SUCCESS: EXIT_FAILURE;
}