arraytest.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include <types.h>
  30. #include <lib.h>
  31. #include <array.h>
  32. #include <test.h>
  33. #define TESTSIZE 73
  34. static
  35. void
  36. testa(struct array *a)
  37. {
  38. int testarray[TESTSIZE];
  39. int i, j, n, r, *p;
  40. for (i=0; i<TESTSIZE; i++) {
  41. testarray[i]=i;
  42. }
  43. n = array_num(a);
  44. KASSERT(n==0);
  45. for (i=0; i<TESTSIZE; i++) {
  46. r = array_add(a, &testarray[i], NULL);
  47. KASSERT(r==0);
  48. n = array_num(a);
  49. KASSERT(n==i+1);
  50. }
  51. n = array_num(a);
  52. KASSERT(n==TESTSIZE);
  53. for (i=0; i<TESTSIZE; i++) {
  54. p = array_get(a, i);
  55. KASSERT(*p == i);
  56. }
  57. n = array_num(a);
  58. KASSERT(n==TESTSIZE);
  59. for (j=0; j<TESTSIZE*4; j++) {
  60. i = random()%TESTSIZE;
  61. p = array_get(a, i);
  62. KASSERT(*p == i);
  63. }
  64. n = array_num(a);
  65. KASSERT(n==TESTSIZE);
  66. for (i=0; i<TESTSIZE; i++) {
  67. array_set(a, i, &testarray[TESTSIZE-i-1]);
  68. }
  69. for (i=0; i<TESTSIZE; i++) {
  70. p = array_get(a, i);
  71. KASSERT(*p == TESTSIZE-i-1);
  72. }
  73. r = array_setsize(a, TESTSIZE/2);
  74. KASSERT(r==0);
  75. for (i=0; i<TESTSIZE/2; i++) {
  76. p = array_get(a, i);
  77. KASSERT(*p == TESTSIZE-i-1);
  78. }
  79. array_remove(a, 1);
  80. for (i=1; i<TESTSIZE/2 - 1; i++) {
  81. p = array_get(a, i);
  82. KASSERT(*p == TESTSIZE-i-2);
  83. }
  84. p = array_get(a, 0);
  85. KASSERT(*p == TESTSIZE-1);
  86. array_setsize(a, 2);
  87. p = array_get(a, 0);
  88. KASSERT(*p == TESTSIZE-1);
  89. p = array_get(a, 1);
  90. KASSERT(*p == TESTSIZE-3);
  91. array_set(a, 1, NULL);
  92. array_setsize(a, 2);
  93. p = array_get(a, 0);
  94. KASSERT(*p == TESTSIZE-1);
  95. p = array_get(a, 1);
  96. KASSERT(p==NULL);
  97. array_setsize(a, TESTSIZE*10);
  98. p = array_get(a, 0);
  99. KASSERT(*p == TESTSIZE-1);
  100. p = array_get(a, 1);
  101. KASSERT(p==NULL);
  102. }
  103. int
  104. arraytest(int nargs, char **args)
  105. {
  106. struct array *a;
  107. (void)nargs;
  108. (void)args;
  109. kprintf("Beginning array test...\n");
  110. a = array_create();
  111. KASSERT(a != NULL);
  112. testa(a);
  113. array_setsize(a, 0);
  114. testa(a);
  115. array_setsize(a, 0);
  116. array_destroy(a);
  117. kprintf("Array test complete\n");
  118. return 0;
  119. }