/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- testa
- arraytest
1 /*
2 * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3 * The President and Fellows of Harvard College.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <types.h>
31 #include <lib.h>
32 #include <array.h>
33 #include <test.h>
34
35 #define TESTSIZE 73
36
37 static
38 void
39 testa(struct array *a)
40 {
41 int testarray[TESTSIZE];
42 int i, j, n, r, *p;
43
44 for (i=0; i<TESTSIZE; i++) {
45 testarray[i]=i;
46 }
47
48 n = array_num(a);
49 KASSERT(n==0);
50
51 for (i=0; i<TESTSIZE; i++) {
52 r = array_add(a, &testarray[i], NULL);
53 KASSERT(r==0);
54 n = array_num(a);
55 KASSERT(n==i+1);
56 }
57 n = array_num(a);
58 KASSERT(n==TESTSIZE);
59
60 for (i=0; i<TESTSIZE; i++) {
61 p = array_get(a, i);
62 KASSERT(*p == i);
63 }
64 n = array_num(a);
65 KASSERT(n==TESTSIZE);
66
67 for (j=0; j<TESTSIZE*4; j++) {
68 i = random()%TESTSIZE;
69 p = array_get(a, i);
70 KASSERT(*p == i);
71 }
72 n = array_num(a);
73 KASSERT(n==TESTSIZE);
74
75 for (i=0; i<TESTSIZE; i++) {
76 array_set(a, i, &testarray[TESTSIZE-i-1]);
77 }
78
79 for (i=0; i<TESTSIZE; i++) {
80 p = array_get(a, i);
81 KASSERT(*p == TESTSIZE-i-1);
82 }
83
84 r = array_setsize(a, TESTSIZE/2);
85 KASSERT(r==0);
86
87 for (i=0; i<TESTSIZE/2; i++) {
88 p = array_get(a, i);
89 KASSERT(*p == TESTSIZE-i-1);
90 }
91
92 array_remove(a, 1);
93
94 for (i=1; i<TESTSIZE/2 - 1; i++) {
95 p = array_get(a, i);
96 KASSERT(*p == TESTSIZE-i-2);
97 }
98 p = array_get(a, 0);
99 KASSERT(*p == TESTSIZE-1);
100
101 array_setsize(a, 2);
102 p = array_get(a, 0);
103 KASSERT(*p == TESTSIZE-1);
104 p = array_get(a, 1);
105 KASSERT(*p == TESTSIZE-3);
106
107 array_set(a, 1, NULL);
108 array_setsize(a, 2);
109 p = array_get(a, 0);
110 KASSERT(*p == TESTSIZE-1);
111 p = array_get(a, 1);
112 KASSERT(p==NULL);
113
114 array_setsize(a, TESTSIZE*10);
115 p = array_get(a, 0);
116 KASSERT(*p == TESTSIZE-1);
117 p = array_get(a, 1);
118 KASSERT(p==NULL);
119 }
120
121 int
122 arraytest(int nargs, char **args)
123 {
124 struct array *a;
125
126 (void)nargs;
127 (void)args;
128
129 kprintf("Beginning array test...\n");
130 a = array_create();
131 KASSERT(a != NULL);
132
133 testa(a);
134
135 array_setsize(a, 0);
136
137 testa(a);
138
139 array_setsize(a, 0);
140 array_destroy(a);
141
142 kprintf("Array test complete\n");
143 return 0;
144 }