-
Notifications
You must be signed in to change notification settings - Fork 0
/
npio_test_c.c
222 lines (182 loc) · 3.91 KB
/
npio_test_c.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <unistd.h>
#include "npio.h"
/* 100-long 64-bit signed array */
void test1()
{
int err;
size_t i;
int64_t *data, total;
npio_Array array;
npio_init_array(&array);
err = npio_load("test1.npy", &array);
if (err)
{
fprintf(stderr, "npio_load: %s\n", strerror(err));
exit(1);
}
printf("data type: ");
if (array.floating_point)
printf("%d-bit float\n", array.bit_width);
else if (array.is_signed)
printf("%d-bit signed integer\n", array.bit_width);
else
printf("%d-bit unsigned integer\n", array.bit_width);
printf("shape: ");
for (i = 0; i < array.dim; ++i)
printf("%ld ", array.shape[i]);
printf("\n");
assert(array.bit_width == 64 && !array.floating_point && array.is_signed);
data = (int64_t*) array.data;
total = 0;
for (i = 0; i < array.size; ++i)
total += data[i];
assert(total == 4950);
npio_free_array(&array);
printf("test1 passed\n");
}
/* 10000-long 32-bit floats in 100x10x10 array */
void test2()
{
int err;
size_t i;
float *data, total;
npio_Array array;
npio_init_array(&array);
err = npio_load("test2.npy", &array);
if (err)
{
fprintf(stderr, "npio_load: %s\n", strerror(err));
exit(1);
}
printf("data type: ");
if (array.floating_point)
printf("%d-bit float\n", array.bit_width);
else if (array.is_signed)
printf("%d-bit signed integer\n", array.bit_width);
else
printf("%d-bit unsigned integer\n", array.bit_width);
printf("shape: ");
for (i = 0; i < array.dim; ++i)
printf("%ld ", array.shape[i]);
printf("\n");
assert(array.bit_width == 32 && array.floating_point);
data = (float*) array.data;
total = 0;
for (i = 0; i < array.size; ++i)
total += data[i];
assert(fabs(total / 5005.37f - 1.0f) < 1e-6f);
npio_free_array(&array);
printf("test2 passed\n");
}
void test3()
{
int i, err;
float *data, total;
npio_Array array;
npio_init_array(&array);
err = npio_load_header("test2.npy", &array);
if (err)
{
fprintf(stderr, "npio_load_header: %s\n", strerror(err));
exit(1);
}
err = npio_load_data(&array);
if (err)
{
fprintf(stderr, "npio_load_data: %s\n", strerror(err));
exit(1);
}
data = (float*) array.data;
total = 0;
for (i = 0; i < array.size; ++i)
total += data[i];
assert(fabs(total / 5005.37f - 1.0f) < 1e-6f);
npio_free_array(&array);
printf("test3 passed\n");
}
void test4()
{
int i, err;
int64_t *data;
float total;
FILE *f;
void *p;
size_t sz;
npio_Array array;
npio_init_array(&array);
f = fopen("test1.npy", "r");
if (!f)
{
fprintf(stderr, "could not open test1.npy\n");
exit(1);
}
fseek(f, 0, SEEK_END);
sz = ftell(f);
fseek(f, 0, SEEK_SET);
p = malloc(sz);
if (fread(p, 1, sz, f) != sz)
{
perror("fread");
exit(1);
}
err = npio_load_mem(p, sz, &array);
if (err)
{
fprintf(stderr, "npio_load_mem: %s\n", strerror(err));
exit(1);
}
fclose(f);
data = (int64_t*) array.data;
total = 0;
for (i = 0; i < array.size; ++i)
total += data[i];
assert(total == 4950);
npio_free_array(&array);
free(p);
printf("test4 passed\n");
}
/* pipe in a file to ourselves to check read-based load */
void test5()
{
int fd, err;
npio_Array array;
size_t sz, i;
FILE* f;
int64_t *data, total;
f = popen("cat test1.npy", "r");
if (f == 0)
{
perror("popen");
exit(1);
}
fd = fileno(f);
npio_init_array(&array);
if ((err = npio_load_fd(fd, &array)))
{
fprintf(stderr, "npio_load_fd: %s\n", strerror(err));
exit(1);
}
data = (int64_t*) array.data;
total = 0;
for (i = 0; i < array.size; ++i)
total += data[i];
assert(total == 4950);
fclose(f);
npio_free_array(&array);
printf("test5 passed\n");
}
int main()
{
test1();
test2();
test3();
test4();
test5();
return 0;
}