• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "nghttp2_pq_test.h"
26 
27 #include <CUnit/CUnit.h>
28 
29 #include "nghttp2_pq.h"
30 
31 typedef struct {
32   nghttp2_pq_entry ent;
33   const char *s;
34 } string_entry;
35 
string_entry_new(const char * s)36 static string_entry *string_entry_new(const char *s) {
37   nghttp2_mem *mem;
38   string_entry *ent;
39 
40   mem = nghttp2_mem_default();
41 
42   ent = nghttp2_mem_malloc(mem, sizeof(string_entry));
43   ent->s = s;
44 
45   return ent;
46 }
47 
string_entry_del(string_entry * ent)48 static void string_entry_del(string_entry *ent) { free(ent); }
49 
pq_less(const void * lhs,const void * rhs)50 static int pq_less(const void *lhs, const void *rhs) {
51   return strcmp(((string_entry *)lhs)->s, ((string_entry *)rhs)->s) < 0;
52 }
53 
test_nghttp2_pq(void)54 void test_nghttp2_pq(void) {
55   int i;
56   nghttp2_pq pq;
57   string_entry *top;
58 
59   nghttp2_pq_init(&pq, pq_less, nghttp2_mem_default());
60   CU_ASSERT(nghttp2_pq_empty(&pq));
61   CU_ASSERT(0 == nghttp2_pq_size(&pq));
62   CU_ASSERT(0 == nghttp2_pq_push(&pq, &string_entry_new("foo")->ent));
63   CU_ASSERT(0 == nghttp2_pq_empty(&pq));
64   CU_ASSERT(1 == nghttp2_pq_size(&pq));
65   top = (string_entry *)nghttp2_pq_top(&pq);
66   CU_ASSERT(strcmp("foo", top->s) == 0);
67   CU_ASSERT(0 == nghttp2_pq_push(&pq, &string_entry_new("bar")->ent));
68   top = (string_entry *)nghttp2_pq_top(&pq);
69   CU_ASSERT(strcmp("bar", top->s) == 0);
70   CU_ASSERT(0 == nghttp2_pq_push(&pq, &string_entry_new("baz")->ent));
71   top = (string_entry *)nghttp2_pq_top(&pq);
72   CU_ASSERT(strcmp("bar", top->s) == 0);
73   CU_ASSERT(0 == nghttp2_pq_push(&pq, &string_entry_new("C")->ent));
74   CU_ASSERT(4 == nghttp2_pq_size(&pq));
75 
76   top = (string_entry *)nghttp2_pq_top(&pq);
77   CU_ASSERT(strcmp("C", top->s) == 0);
78   string_entry_del(top);
79   nghttp2_pq_pop(&pq);
80 
81   CU_ASSERT(3 == nghttp2_pq_size(&pq));
82 
83   top = (string_entry *)nghttp2_pq_top(&pq);
84   CU_ASSERT(strcmp("bar", top->s) == 0);
85   nghttp2_pq_pop(&pq);
86   string_entry_del(top);
87 
88   top = (string_entry *)nghttp2_pq_top(&pq);
89   CU_ASSERT(strcmp("baz", top->s) == 0);
90   nghttp2_pq_pop(&pq);
91   string_entry_del(top);
92 
93   top = (string_entry *)nghttp2_pq_top(&pq);
94   CU_ASSERT(strcmp("foo", top->s) == 0);
95   nghttp2_pq_pop(&pq);
96   string_entry_del(top);
97 
98   CU_ASSERT(nghttp2_pq_empty(&pq));
99   CU_ASSERT(0 == nghttp2_pq_size(&pq));
100   CU_ASSERT(NULL == nghttp2_pq_top(&pq));
101 
102   /* Add bunch of entry to see realloc works */
103   for (i = 0; i < 10000; ++i) {
104     CU_ASSERT(0 == nghttp2_pq_push(&pq, &string_entry_new("foo")->ent));
105     CU_ASSERT((size_t)(i + 1) == nghttp2_pq_size(&pq));
106   }
107   for (i = 10000; i > 0; --i) {
108     top = (string_entry *)nghttp2_pq_top(&pq);
109     CU_ASSERT(NULL != top);
110     nghttp2_pq_pop(&pq);
111     string_entry_del(top);
112     CU_ASSERT((size_t)(i - 1) == nghttp2_pq_size(&pq));
113   }
114 
115   nghttp2_pq_free(&pq);
116 }
117 
118 typedef struct {
119   nghttp2_pq_entry ent;
120   int key;
121   int val;
122 } node;
123 
node_less(const void * lhs,const void * rhs)124 static int node_less(const void *lhs, const void *rhs) {
125   node *ln = (node *)lhs;
126   node *rn = (node *)rhs;
127   return ln->key < rn->key;
128 }
129 
node_update(nghttp2_pq_entry * item,void * arg)130 static int node_update(nghttp2_pq_entry *item, void *arg) {
131   node *nd = (node *)item;
132   (void)arg;
133 
134   if ((nd->key % 2) == 0) {
135     nd->key *= -1;
136     return 1;
137   } else {
138     return 0;
139   }
140 }
141 
test_nghttp2_pq_update(void)142 void test_nghttp2_pq_update(void) {
143   nghttp2_pq pq;
144   node nodes[10];
145   int i;
146   node *nd;
147   int ans[] = {-8, -6, -4, -2, 0, 1, 3, 5, 7, 9};
148 
149   nghttp2_pq_init(&pq, node_less, nghttp2_mem_default());
150 
151   for (i = 0; i < (int)(sizeof(nodes) / sizeof(nodes[0])); ++i) {
152     nodes[i].key = i;
153     nodes[i].val = i;
154     nghttp2_pq_push(&pq, &nodes[i].ent);
155   }
156 
157   nghttp2_pq_update(&pq, node_update, NULL);
158 
159   for (i = 0; i < (int)(sizeof(nodes) / sizeof(nodes[0])); ++i) {
160     nd = (node *)nghttp2_pq_top(&pq);
161     CU_ASSERT(ans[i] == nd->key);
162     nghttp2_pq_pop(&pq);
163   }
164 
165   nghttp2_pq_free(&pq);
166 }
167 
push_nodes(nghttp2_pq * pq,node * dest,size_t n)168 static void push_nodes(nghttp2_pq *pq, node *dest, size_t n) {
169   size_t i;
170   for (i = 0; i < n; ++i) {
171     dest[i].key = (int)i;
172     dest[i].val = (int)i;
173     nghttp2_pq_push(pq, &dest[i].ent);
174   }
175 }
176 
check_nodes(nghttp2_pq * pq,size_t n,int * ans_key,int * ans_val)177 static void check_nodes(nghttp2_pq *pq, size_t n, int *ans_key, int *ans_val) {
178   size_t i;
179   for (i = 0; i < n; ++i) {
180     node *nd = (node *)nghttp2_pq_top(pq);
181     CU_ASSERT(ans_key[i] == nd->key);
182     CU_ASSERT(ans_val[i] == nd->val);
183     nghttp2_pq_pop(pq);
184   }
185 }
186 
test_nghttp2_pq_remove(void)187 void test_nghttp2_pq_remove(void) {
188   nghttp2_pq pq;
189   node nodes[10];
190   int ans_key1[] = {1, 2, 3, 4, 5};
191   int ans_val1[] = {1, 2, 3, 4, 5};
192   int ans_key2[] = {0, 1, 2, 4, 5};
193   int ans_val2[] = {0, 1, 2, 4, 5};
194   int ans_key3[] = {0, 1, 2, 3, 4};
195   int ans_val3[] = {0, 1, 2, 3, 4};
196 
197   nghttp2_pq_init(&pq, node_less, nghttp2_mem_default());
198 
199   push_nodes(&pq, nodes, 6);
200 
201   nghttp2_pq_remove(&pq, &nodes[0].ent);
202 
203   check_nodes(&pq, 5, ans_key1, ans_val1);
204 
205   nghttp2_pq_free(&pq);
206 
207   nghttp2_pq_init(&pq, node_less, nghttp2_mem_default());
208 
209   push_nodes(&pq, nodes, 6);
210 
211   nghttp2_pq_remove(&pq, &nodes[3].ent);
212 
213   check_nodes(&pq, 5, ans_key2, ans_val2);
214 
215   nghttp2_pq_free(&pq);
216 
217   nghttp2_pq_init(&pq, node_less, nghttp2_mem_default());
218 
219   push_nodes(&pq, nodes, 6);
220 
221   nghttp2_pq_remove(&pq, &nodes[5].ent);
222 
223   check_nodes(&pq, 5, ans_key3, ans_val3);
224 
225   nghttp2_pq_free(&pq);
226 }
227