-
Notifications
You must be signed in to change notification settings - Fork 1
/
CVectorBasicTest.c
172 lines (157 loc) · 7.14 KB
/
CVectorBasicTest.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* CVectorBasicTest.c
* CVector
*
* Created by Herbert J. Bernstein on 12/04/08.
* Copyright 2008 Herbert J. Bernstein. All rights reserved.
*
*/
/**********************************************************************
* *
* YOU MAY REDISTRIBUTE THE CVector API UNDER THE TERMS OF THE LGPL *
* *
**********************************************************************/
/************************* LGPL NOTICES *******************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
* MA 02110-1301 USA *
* *
**********************************************************************/
#ifdef USE_LOCAL_HEADERS
#include "CVector.h"
#else
#include <CVector.h>
#endif
#include <stdio.h>
typedef long unsigned int LUI;
CVectorHandle vectorhandle;
int main(int argc, char **argv) {
size_t index;
double element;
double * elementptr;
void * velementptr;
unsigned int flags;
size_t size, capacity;
FILE * out;
out = stdout;
if (argc > 1) {
out = fopen(argv[1],"w+");
if (!out) {
fprintf(stderr," Failed to open output file %s\n", argv[1]);
exit(1);
}
}
fprintf(out," Test 0: Create vector and load it with 1000000 elements.\n");
CVectorCreate(&vectorhandle,sizeof(double),1);
for (index=0; index<1000000; index++) {
element = (double)index;
CVectorAddElement(vectorhandle, &element);
if (!(index&0x1FFFF)) {
if (CVectorGetCapacity(vectorhandle,&capacity) ){
fprintf(stderr,"Call to CVectorGetCapacity failed.\n");
}
if (CVectorGetSize(vectorhandle,&size)) {
fprintf(stderr,"Call to CVectorGetSize failed.\n");
}
if (size != index+1) {
fprintf(stderr,"Size mismatch index+1 %lu, size %lu.\n",(LUI)index+1,(LUI)size);
}
fprintf(out,"Size %lu, capacity %lu.\n", (LUI)size, (LUI)capacity);
}
}
fprintf(out," Test 1: Reference elements of CVector as an array.\n");
for (index=0; index<1000000; index++) {
if ((double)index != ((double *)(vectorhandle->array))[index]) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, ((double *)(vectorhandle->array))[index]);
}
}
fprintf(out," Test 2: Reference elements of CVector via CVectorGetElement.\n");
for (index=0; index<(size_t)1000000; index++) {
if (CVectorGetElement(vectorhandle, &element, index))
fprintf(stderr," Failed CVectorGetElement, index = %lu\n",(LUI)index);
if ((double)index != element) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, element);
}
}
/* The next block will referenece the array via pointers
This will also set the CVECTOR_FLAGS_NO_RELOCATION
Will then reset the flag.
*/
fprintf(out," Test 3: Reference elements of CVector via CVectorGetElementptr.\n");
for (index=0; index<(size_t)1000000; index++) {
if (CVectorGetElementptr(vectorhandle, &velementptr, index))
fprintf(stderr," Failed CVectorGetElement, index = %lu\n",(LUI)index);
elementptr = (double *)velementptr;
if (velementptr != CVectorElementAt(vectorhandle, index)) {
fprintf(stderr," Failed CVectorGetElementAt, index = %lu\n",(LUI)index);
}
if ((double)index != *elementptr) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, *elementptr);
}
}
if (CVectorGetFlags(vectorhandle,&flags))
fprintf(stderr,"Call to CVectorGetFlags failed.\n");
if ( !(flags&CVECTOR_FLAGS_NO_RELOCATION) )
fprintf(stderr,"CVECTOR_FLAGS_NO_RELOCATION not set by CVectorGetElementptr.\n");
flags = 0;
if (CVectorSetFlags(vectorhandle,flags))
fprintf(stderr,"Call to CVectorSetFlags failed.\n");
fprintf(out," Test 4: Set elements of CVector as an array\n");
for (index=0; index<(size_t)1000000; index++) {
((double *)(vectorhandle->array))[index] = (double)(1000000-index);
}
for (index=0; index<(size_t)1000000; index++) {
if ((double)(1000000-index) != ((double *)(vectorhandle->array))[index]) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, ((double *)(vectorhandle->array))[index]);
}
}
fprintf(out," Test 5: Set elements of CVector via CVectorSetElement.\n");
for (index=0; index<(size_t)1000000; index++) {
element = (double)(index-1000000);
if (CVectorSetElement(vectorhandle,&element,1000000-index))
fprintf(stderr," Failed CVectorSetElement, index = %lu\n",(LUI)(1000000-index));
}
for (index=0; index<(size_t)1000000; index++) {
if ((double)(index-1000000) != ((double *)(vectorhandle->array))[1000000-index]) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, ((double *)(vectorhandle->array))[1000000-index]);
}
}
fprintf(out," Test 6: Remove elements of CVector via CVectorRemoveElement.\n");
for (index=0; index<(size_t)2000; index++) {
((double *)(vectorhandle->array))[index] = (double)(index);
}
for (index=0; index<(size_t)1000; index++) {
if (CVectorRemoveElement(vectorhandle,index))
fprintf(stderr," Failed CVectorRemoveElement, index = %lu\n",(LUI)index);
}
for (index=0; index<(size_t)1000; index++) {
if ((double)(index*2+1) != ((double *)(vectorhandle->array))[index]) {
fprintf(stderr," data mismatch array[%lu] = %g\n",
(LUI)index, ((double *)(vectorhandle->array))[index]);
}
}
fprintf(out," Test 7: Remove all elements of CVector via CVectorClear.\n");
if (CVectorClear(vectorhandle))
fprintf(stderr," Failed CVectorClear\n");
if (CVectorSize(vectorhandle) !=0) {
fprintf(stderr," CVectorClear failed to set size to zero\n");
}
return 0;
}