regen.sh: Use libtoolize -i, and .gitignore generated build-tools
[openafs.git] / tests / opr / dict-t.c
1 /*
2  * Copyright (c) 2012 Your File System Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include <afsconfig.h>
26 #include <afs/param.h>
27
28 #include <tests/tap/basic.h>
29 #include <opr/dict.h>
30
31 struct myentry {
32     int value;
33     struct opr_queue d;
34 };
35
36 int
37 find(struct opr_dict *dict, int value, struct myentry **entryp)
38 {
39     struct myentry *ent;
40     struct opr_queue *cursor;
41     int length = 0;
42
43     for (opr_dict_ScanBucket(dict, value, cursor)) {
44         ent = opr_queue_Entry(cursor, struct myentry, d);
45         length++;
46
47         if (ent->value == value) {
48            if (entryp)
49                 *entryp = ent;
50            return length;
51         }
52     }
53     return 0;
54 }
55
56 int
57 main(void)
58 {
59     struct opr_dict *dict;
60     struct myentry *entry;
61     int members[] = {1,2,3,4,5,6,7,8,9,10,17,0};
62     int i;
63
64     plan(10);
65
66     ok(opr_dict_Init(3) == NULL,
67        "Initialising a dictionary with a bad size fails");
68
69     dict = opr_dict_Init(8);
70     ok(dict != NULL,
71        "Initialising a dictionary succeeds");
72
73     for (i = 0; members[i] !=0; i++) {
74         entry = malloc(sizeof(struct myentry));
75         entry->value = members[i];
76         opr_dict_Append(dict, entry->value, &entry->d);
77     }
78     ok(1, "Hash populated successfully");
79
80     is_int(1, find(dict, 1, NULL),
81            "Entry 1 is first in hash chain");
82     is_int(2, find(dict, 9, NULL),
83            "Entry 9 is second in hash chain");
84     is_int(3, find(dict, 17, NULL),
85            "Entry 17 is third in hash chain");
86     is_int(1, find(dict, 2, NULL),
87            "Entry 2 is first in hash chain");
88     is_int(1, find(dict, 8, NULL),
89            "Entry 8 is first in hash chain");
90
91     find(dict, 17, &entry);
92     ok(entry != NULL && entry->value == 17, "Retrieved entry 17");
93     opr_dict_Promote(dict, entry->value, &entry->d);
94     is_int(1, find(dict, 17, NULL),
95            "Entry 17 is first in hash chain following promotion");
96
97     return 0;
98 }