summaryrefslogtreecommitdiffstats
path: root/clib/test/table.c
blob: 62253dfa8d90a3983d95948da29635f620931836 (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
/*
 * Copyright (c) International Business Machines Corp., 2014
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

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

#include <clib/table.h>
#include <clib/table_iter.h>

#define COLS 5

int main(void) {
    table_t t = INIT_TABLE;
    table_init(&t, "table", COLS);

    const char * str = "column4 is a really long assed string";

    table_name(&t, 0, "column0");
    table_name(&t, 1, "column1");
    table_name(&t, 2, "column2");
    table_name(&t, 3, "column3");
    table_name(&t, 4, str);

    printf("%s\n", table_name(&t, 4));
    printf("cols: %d\n", table_columns(&t));
    printf("rows: %d\n", table_rows(&t));

    table_dump(&t, stdout);

    table_iter_t it;
    table_iter_init(&it, &t, TI_FLAG_FWD);

    size_t r, c;

    value_t * val;
    table_for_each(&it, val) {
        for (c=0; c<COLS; c++)
            value_dump(val+c, stdout);
    }
    printf("xxx =================\n");

    value_t v[COLS];

    for (r=0; r<COLS; r++) {
        for (c=0; c<COLS; c++) {
            printf("t[%d][%d] --> %d\n", r, c, r * COLS + c);
            value_i32(v+c, r * COLS + c);
     	    value_dump(v+c, stdout);
        }
        table_row(&t, r, v);
    }
    table_dump(&t, stdout);
    printf("yyy =================\n");

    table_for_each(&it, val) {
        for (c=0; c<COLS; c++)
            value_dump(val+c, stdout);
    }
    printf("zzz =================\n");

    FILE * f = fopen("table.bin", "w+");
    table_serialize(&t);
    table_save(&t, f);
    table_delete(&t);
    fclose(f);

    printf("111 =================\n");

    f = fopen("table.bin", "r+");
    table_load(&t, f);
    table_deserialize(&t);
    table_dump(&t, stdout);
    fclose(f);

    table_iter_init(&it, &t, TI_FLAG_FWD);
    table_for_each(&it, val) {
        for (c=0; c<COLS; c++)
            value_dump(val+c, stdout);
    }
    printf("aaa =================\n");

    table_dump(&t, stdout);
    table_delete(&t);

    return 0;
}
OpenPOWER on IntegriCloud