File: test_hist.sl

package info (click to toggle)
slang2 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,588 kB
  • ctags: 10,558
  • sloc: ansic: 95,506; sh: 3,277; makefile: 945; pascal: 143
file content (233 lines) | stat: -rw-r--r-- 5,524 bytes parent folder | download
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
223
224
225
226
227
228
229
230
231
232
233
() = evalfile ("./test.sl");
require ("histogram");

private variable Random_Number = _time ();
private define urand_1 (x)
{
   Random_Number = typecast (Random_Number * 69069UL + 1013904243UL, UInt32_Type);
   return Random_Number/4294967296.0;
}
private define urand (n)
{
   if (n == 0)
     return Double_Type[0];

   return array_map (Double_Type, &urand_1, [1:n]);
}

define test_hist1d (n, m)
{
   variable pts = urand (n); pts = pts[array_sort(pts)];
   variable edges = [1:m]/(1.1*m);
   variable max_edges;
   variable rev_indices;

   variable h = hist1d (pts, edges, &rev_indices);

   if (length (edges) == 0)
     {
	if (length (h) != 0)
	  failed ("hist1d(%S,%S): hist1d returned wrong size histogram");

	if (length (rev_indices) != 0)
	  failed ("hist1d(%S,%S): hist1d returned wrong size rev-ind array");

	return;
     }

   max_edges = max(edges);
   variable len = length (where (pts >= max_edges));
   if (len != h[-1])
     failed ("hist1d(%S,%S); last bin: expect %d, found %d",
	     n, m, len, h[-1]);

   len = length (where (pts < edges[0]));
   if (len + sum (h) != length (pts))
     failed ("hist1d: total number expect is wrong");

   _for (0, m-1, 1)
     {
	variable i = ();
	variable j = rev_indices[i];

	!if (length (j))
	  continue;

	if (length (where ((j < 0) or (j >= n))))
	  failed ("hist1d: reverse index out of range");

	variable p = pts[j];

	if (length (where (p < edges[i])))
	  failed ("hist1d: reverse index problem 2");

	if (i != m-1)
	  {
	     if (length (where (p >= edges[i+1])))
	       failed ("hist1d: reverse index problem 2");
	  }
     }
}

define test_hist1d_uc (n, edges)
{
   variable pts = typecast (256 * urand (n), UChar_Type);
   variable rev_indices;

   variable h1 = hist1d (pts, edges, &rev_indices);
   variable h2 = hist1d (pts, edges);

   if (length (where (h1 != h2)))
     failed ("hist1d on unsigned chars");
}

define do_test_hist1d (n, m)
{
   test_hist1d (n, m);
   test_hist1d_uc (n, [-3:0]);
   test_hist1d_uc (n*10, [-3,0.01]);
   test_hist1d_uc (n*10, [-3:10]);
   test_hist1d_uc (n*10, [-3:10:0.1]);
   test_hist1d_uc (n*10, [0:254]);
   test_hist1d_uc (n*10, [0:254:0.1]);
   test_hist1d_uc (n*10, [0:255]);
   test_hist1d_uc (n*10, [0:256]);
   test_hist1d_uc (n*10, [-1,256]);
   test_hist1d_uc (n*10, [255,256]);
   test_hist1d_uc (n*10, [255:256:0.1]);
   test_hist1d_uc (n*10, [255.1:270]);
   test_hist1d_uc (n*10, [256:270]);
   test_hist1d_uc (n*10, [254.9,255.0]);
   test_hist1d_uc (n*10, [254.9,255.01]);
}

define test_hist2d (num, nr, nc) %{{{
{
   variable r = urand(num);
   variable c = urand(num);

   variable gr, gc;
   gr = [1:nr]/(1.1*nr);
   gc = [1:nc]/(1.1*nc);

   variable rev, img;
   img = hist2d (r, c, gr, gc, &rev);

   % all data points got binned
   variable i = where ((r >= gr[0]) and (c >= gc[0]));
   if (sum(img) != length(i))
     failed ("histogram sum");

   % the reverse indices include every point
   _for (0, nr-1, 1)
     {
	variable ir = ();
	variable rlo, rhi;
	rlo = gr[ir];
	if (ir == nr-1)
	  rhi = 10;
	else
	  rhi = gr[ir+1];

	_for (0, nc-1, 1)
	  {
	     variable ic = ();
	     variable clo, chi;

	     i = rev[ir, ic];
	     if (0 == length (i))
	       continue;

	     clo = gc[ic];
	     if (ic == nc-1)
	       chi = 10;
	     else
	       chi = gc[ic+1];

	     if (length (where ((rlo > r[i]) or (r[i] >= rhi)
				or (clo > c[i]) or (c[i] >= chi))))
	       failed ("hist2d: Reverse index problem");
	  }
     }
}

do_test_hist1d (20, 5);
do_test_hist1d (20, 4);
do_test_hist1d (20, 3);
do_test_hist1d (20, 2);
do_test_hist1d (20, 1);

do_test_hist1d (20, 500);
do_test_hist1d (20, 400);
do_test_hist1d (20, 300);
do_test_hist1d (20, 200);
do_test_hist1d (20, 100);

% Now test oddball cases

do_test_hist1d (0, 5);
do_test_hist1d (1, 5);
do_test_hist1d (0, 1);
do_test_hist1d (0, 0);

test_hist2d (20, 1, 1);
test_hist2d (200, 1, 3);
test_hist2d (20, 10, 20);
test_hist2d (20, 10, 30);
test_hist2d (20000, 10, 30);

private variable Test_Number = 0;
private define test_rebin (new_grid, old_grid, input_h, sum_ok, expected)
{
   variable new_h = hist1d_rebin (new_grid, old_grid, input_h);

   Test_Number++;

   if (sum_ok)
     {
	if (sum (new_h) != sum (input_h))
	  failed ("hist1d_rebin[%d]: sum: %S != %S",
		  Test_Number, sum(new_h), sum(input_h));
     }
   if (expected != NULL)
     {
	variable i = where (expected != new_h);
	if (length (i))
	  {
	     i = i[0];
	     failed ("hist1d_rebin[%d]: expected %S in bin %d, found %S (diff=%S)",
		     Test_Number, expected[i], i, new_h[i], expected[i] - new_h[i]);
	  }
     }
}

private define test_module (module_name)
{
   testing_module (module_name);

   variable g0 = [0,1,2,3,4,5,6];
   variable h0 = [1,2,3,4,5,6,7];

%test_rebin ([0,1], [0], [1], 1, [0.5, 0.5]);

   test_rebin ([0,2,4,6], g0, h0, 1, [3,7,11,7]);
   test_rebin ([0,2,4], g0, h0, 1, [3,7,18]);
   test_rebin ([0], g0, h0, 1, [28]);
   test_rebin ([-1], g0, h0, 1, [28]);
   test_rebin ([-1,0], g0, h0, 1, [0,28]);
   test_rebin ([-1,0,6,7,8], g0, h0, 1, [0,21,0,0,7]);
   test_rebin ([-1,0,0.5,7,8], g0, h0, 1, [0,0.5, 20.5,0,7]);
   test_rebin ([1.5,2.5], g0, h0, 0, [2.5,23.5]);
   test_rebin ([8], g0, h0, 0, [7]);
   test_rebin ([-1,9], g0, h0, 1, [21, 7]);
   test_rebin ([1,10], [-4], [12], 1, [0, 12]);
   test_rebin (Double_Type[0], g0, h0, 0, Double_Type[0]);
   test_rebin ([1:10], [1,5,10], [1,2,3], 1,
	       [0.25,0.25,0.25,0.25,0.4,0.4,0.4,0.4,0.4,3]);
}

define slsh_main ()
{
   test_module ("hist");
   end_test ();
}