/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <types.h>30#include <lib.h>31#include <array.h>32#include <test.h>3334#define TESTSIZE 733536static37void38testa(struct array *a)39{40int testarray[TESTSIZE];41int i, j, n, r, *p;4243for (i=0; i<TESTSIZE; i++) {44testarray[i]=i;45}4647n = array_num(a);48KASSERT(n==0);4950for (i=0; i<TESTSIZE; i++) {51r = array_add(a, &testarray[i], NULL);52KASSERT(r==0);53n = array_num(a);54KASSERT(n==i+1);55}56n = array_num(a);57KASSERT(n==TESTSIZE);5859for (i=0; i<TESTSIZE; i++) {60p = array_get(a, i);61KASSERT(*p == i);62}63n = array_num(a);64KASSERT(n==TESTSIZE);6566for (j=0; j<TESTSIZE*4; j++) {67i = random()%TESTSIZE;68p = array_get(a, i);69KASSERT(*p == i);70}71n = array_num(a);72KASSERT(n==TESTSIZE);7374for (i=0; i<TESTSIZE; i++) {75array_set(a, i, &testarray[TESTSIZE-i-1]);76}7778for (i=0; i<TESTSIZE; i++) {79p = array_get(a, i);80KASSERT(*p == TESTSIZE-i-1);81}8283r = array_setsize(a, TESTSIZE/2);84KASSERT(r==0);8586for (i=0; i<TESTSIZE/2; i++) {87p = array_get(a, i);88KASSERT(*p == TESTSIZE-i-1);89}9091array_remove(a, 1);9293for (i=1; i<TESTSIZE/2 - 1; i++) {94p = array_get(a, i);95KASSERT(*p == TESTSIZE-i-2);96}97p = array_get(a, 0);98KASSERT(*p == TESTSIZE-1);99100array_setsize(a, 2);101p = array_get(a, 0);102KASSERT(*p == TESTSIZE-1);103p = array_get(a, 1);104KASSERT(*p == TESTSIZE-3);105106array_set(a, 1, NULL);107array_setsize(a, 2);108p = array_get(a, 0);109KASSERT(*p == TESTSIZE-1);110p = array_get(a, 1);111KASSERT(p==NULL);112113array_setsize(a, TESTSIZE*10);114p = array_get(a, 0);115KASSERT(*p == TESTSIZE-1);116p = array_get(a, 1);117KASSERT(p==NULL);118}119120int121arraytest(int nargs, char **args)122{123struct array *a;124125(void)nargs;126(void)args;127128kprintf("Beginning array test...\n");129a = array_create();130KASSERT(a != NULL);131132testa(a);133134array_setsize(a, 0);135136testa(a);137138array_setsize(a, 0);139array_destroy(a);140141kprintf("Array test complete\n");142return 0;143}144145146