os161-1.99
 All Data Structures
arraytest.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #include <types.h>
00031 #include <lib.h>
00032 #include <array.h>
00033 #include <test.h>
00034 
00035 #define TESTSIZE 73
00036 
00037 static
00038 void
00039 testa(struct array *a)
00040 {
00041         int testarray[TESTSIZE];
00042         int i, j, n, r, *p;
00043 
00044         for (i=0; i<TESTSIZE; i++) {
00045                 testarray[i]=i;
00046         }
00047 
00048         n = array_num(a);
00049         KASSERT(n==0);
00050 
00051         for (i=0; i<TESTSIZE; i++) {
00052                 r = array_add(a, &testarray[i], NULL);
00053                 KASSERT(r==0);
00054                 n = array_num(a);
00055                 KASSERT(n==i+1);
00056         }
00057         n = array_num(a);
00058         KASSERT(n==TESTSIZE);
00059 
00060         for (i=0; i<TESTSIZE; i++) {
00061                 p = array_get(a, i);
00062                 KASSERT(*p == i);
00063         }
00064         n = array_num(a);
00065         KASSERT(n==TESTSIZE);
00066 
00067         for (j=0; j<TESTSIZE*4; j++) {
00068                 i = random()%TESTSIZE;
00069                 p = array_get(a, i);
00070                 KASSERT(*p == i);
00071         }
00072         n = array_num(a);
00073         KASSERT(n==TESTSIZE);
00074 
00075         for (i=0; i<TESTSIZE; i++) {
00076                 array_set(a, i, &testarray[TESTSIZE-i-1]);
00077         }
00078 
00079         for (i=0; i<TESTSIZE; i++) {
00080                 p = array_get(a, i);
00081                 KASSERT(*p == TESTSIZE-i-1);
00082         }
00083 
00084         r = array_setsize(a, TESTSIZE/2);
00085         KASSERT(r==0);
00086 
00087         for (i=0; i<TESTSIZE/2; i++) {
00088                 p = array_get(a, i);
00089                 KASSERT(*p == TESTSIZE-i-1);
00090         }
00091 
00092         array_remove(a, 1);
00093 
00094         for (i=1; i<TESTSIZE/2 - 1; i++) {
00095                 p = array_get(a, i);
00096                 KASSERT(*p == TESTSIZE-i-2);
00097         }
00098         p = array_get(a, 0);
00099         KASSERT(*p == TESTSIZE-1);
00100 
00101         array_setsize(a, 2);
00102         p = array_get(a, 0);
00103         KASSERT(*p == TESTSIZE-1);
00104         p = array_get(a, 1);
00105         KASSERT(*p == TESTSIZE-3);
00106 
00107         array_set(a, 1, NULL);
00108         array_setsize(a, 2);
00109         p = array_get(a, 0);
00110         KASSERT(*p == TESTSIZE-1);
00111         p = array_get(a, 1);
00112         KASSERT(p==NULL);
00113 
00114         array_setsize(a, TESTSIZE*10);
00115         p = array_get(a, 0);
00116         KASSERT(*p == TESTSIZE-1);
00117         p = array_get(a, 1);
00118         KASSERT(p==NULL);
00119 }
00120 
00121 int
00122 arraytest(int nargs, char **args)
00123 {
00124         struct array *a;
00125 
00126         (void)nargs;
00127         (void)args;
00128 
00129         kprintf("Beginning array test...\n");
00130         a = array_create();
00131         KASSERT(a != NULL);
00132 
00133         testa(a);
00134 
00135         array_setsize(a, 0);
00136 
00137         testa(a);
00138 
00139         array_setsize(a, 0);
00140         array_destroy(a);
00141 
00142         kprintf("Array test complete\n");
00143         return 0;
00144 }
 All Data Structures