summaryrefslogtreecommitdiffstats
path: root/test/unit/array.c
blob: 65ad1d803dd8c861b02f84b3c647c77d12de64c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 **********************************************************************
 * Copyright (C) Miroslav Lichvar  2023
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 **********************************************************************
 */

#include <array.c>
#include <util.h>
#include "test.h"

void
test_unit(void)
{
  unsigned int i, j, k, k2, l, es, s;
  unsigned char *el1, el2[20];
  ARR_Instance a;

  for (i = 0; i < 1000; i++) {
    es = random() % sizeof (el2) + 1;

    a = ARR_CreateInstance(es);

    TEST_CHECK(ARR_GetSize(a) == 0);

    for (j = 0; j < 100; j++) {
      s = ARR_GetSize(a);

      switch (random() % 6) {
        case 0:
          el1 = ARR_GetNewElement(a);
          TEST_CHECK(ARR_GetSize(a) == s + 1);
          memset(el1, s % 256, es);
          TEST_CHECK(ARR_GetElement(a, s) == el1);
          break;
        case 1:
          for (k = 0; k < s; k++) {
            el1 = ARR_GetElement(a, k);
            for (l = 0; l < es; l++)
              TEST_CHECK(el1[l] == k % 256);
          }
          break;
        case 2:
          if (s == 0)
            break;
          TEST_CHECK(ARR_GetElements(a) == ARR_GetElement(a, 0));
          break;
        case 3:
          memset(el2, s % 256, es);
          ARR_AppendElement(a, el2);
          TEST_CHECK(ARR_GetSize(a) == s + 1);
          break;
        case 4:
          if (s == 0)
            break;
          k2 = random() % s;
          ARR_RemoveElement(a, k2);
          TEST_CHECK(ARR_GetSize(a) == s - 1);
          for (k = k2; k < s - 1; k++) {
            el1 = ARR_GetElement(a, k);
            for (l = 0; l < es; l++) {
              TEST_CHECK(el1[l] == (k + 1) % 256);
              el1[l] = k % 256;
            }
          }
          break;
        case 5:
          k2 = random() % 1000;
          ARR_SetSize(a, k2);
          TEST_CHECK(ARR_GetSize(a) == k2);
          for (k = s; k < k2; k++) {
            el1 = ARR_GetElement(a, k);
            for (l = 0; l < es; l++)
              el1[l] = k % 256;
          }
          break;
        default:
          assert(0);
      }
    }

    ARR_DestroyInstance(a);
  }
}