summaryrefslogtreecommitdiffstats
path: root/tools/regression/lib/libutil/test-humanize_number.c
blob: 24f197dac1ed80a3ec203c7088091335aec86b5e (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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*-
 * Copyright 2012 Clifton Royston
 * Copyright 2013 John-Mark Gurney
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 *
 */

#include <sys/types.h>
#include <stdlib.h>
#include <libutil.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include <unistd.h>
#include <limits.h>

extern char * optarg;

#define	MAX_STR_FLAGS_RESULT	80
#define MAX_INT_STR_DIGITS	12

static const int64_t halfExabyte = (int64_t)500*1000*1000*1000*1000*1000L;

static struct {
	int retval;
	const char *res;
	int64_t num;
	int flags;
	int scale;
} test_args[] = {
	/* tests 0-13 test 1000 suffixes */
	{ 2, "0 ", (int64_t)0L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "1 k", (int64_t)500L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "1 G", (int64_t)500*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "1 T", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "1 P", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "1 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 2, "1 ", (int64_t)1L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "2 k", (int64_t)1500L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "2 M", (int64_t)1500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "2 G", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "2 T", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "2 P", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "2 E", (int64_t)1500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },

	/* tests 14-27 test 1024 suffixes */
	{ 2, "0 ", (int64_t)0L, 0, HN_AUTOSCALE },
	{ 3, "1 K", (int64_t)512L, 0, HN_AUTOSCALE },
	{ 3, "1 M", (int64_t)512*1024L, 0, HN_AUTOSCALE },
	{ 3, "1 G", (int64_t)512*1024*1024L, 0, HN_AUTOSCALE },
	{ 3, "1 T", (int64_t)512*1024*1024*1024L, 0, HN_AUTOSCALE },
	{ 3, "1 P", (int64_t)512*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
	{ 3, "1 E", (int64_t)512*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
	{ 2, "1 ", (int64_t)1L, 0, HN_AUTOSCALE },
	{ 3, "2 K", (int64_t)1536L, 0, HN_AUTOSCALE },
	{ 3, "2 M", (int64_t)1536*1024L, 0, HN_AUTOSCALE },
	{ 3, "2 G", (int64_t)1536*1024*1024L, 0, HN_AUTOSCALE },
	{ 3, "2 T", (int64_t)1536*1024*1024*1024L, 0, HN_AUTOSCALE },
	{ 3, "2 P", (int64_t)1536*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
	{ 3, "2 E", (int64_t)1536*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },

	/* tests 28-37 test rounding */
	{ 3, "0 M", (int64_t)500*1000L-1, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "1 M", (int64_t)1000*1000L + 500*1000L-1, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "2 M", (int64_t)1000*1000L + 500*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 3, "0 K", (int64_t)512L-1, 0, HN_AUTOSCALE },
	{ 3, "1 K", (int64_t)512L, 0, HN_AUTOSCALE },
	{ 3, "0 M", (int64_t)512*1024L-1, 0, HN_AUTOSCALE },
	{ 3, "1 M", (int64_t)512*1024L, 0, HN_AUTOSCALE },
	{ 3, "1 M", (int64_t)1024*1024L + 512*1024L-1, 0, HN_AUTOSCALE },
	{ 3, "2 M", (int64_t)1024*1024L + 512*1024L, 0, HN_AUTOSCALE },

	/* tests 38-61 test specific scale factors with 1000 divisor */
	{ 3, "0 k", (int64_t)0L, HN_DIVISOR_1000, 1 },
	{ 3, "1 k", (int64_t)500L, HN_DIVISOR_1000, 1 },
	{ 3, "0 M", (int64_t)500L, HN_DIVISOR_1000, 2 },
	{ 3, "1 M", (int64_t)500*1000L, HN_DIVISOR_1000, 2 },
	{ 3, "0 G", (int64_t)500*1000L, HN_DIVISOR_1000, 3 },
	{ 3, "1 G", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 3 },
	{ 3, "0 T", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 4 },
	{ 3, "1 T", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, 4 },
	{ 3, "0 P", (int64_t)500*1000*1000*1000L, HN_DIVISOR_1000, 5 },
	{ 3, "1 P", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, 5 },
	{ 3, "0 E", (int64_t)500*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
	{ 3, "1 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
	{ 3, "0 k", (int64_t)1L, HN_DIVISOR_1000, 1 },
	{ 3, "2 k", (int64_t)1500L, HN_DIVISOR_1000, 1 },
	{ 3, "0 M", (int64_t)1500L, HN_DIVISOR_1000, 2 },
	{ 3, "2 M", (int64_t)1500*1000L, HN_DIVISOR_1000, 2 },
	{ 3, "0 G", (int64_t)1500*1000L, HN_DIVISOR_1000, 3 },
	{ 3, "2 G", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 3 },
	{ 3, "0 T", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 4 },
	{ 3, "2 T", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, 4 },
	{ 3, "0 P", (int64_t)1500*1000*1000*1000L, HN_DIVISOR_1000, 5 },
	{ 3, "2 P", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, 5 },
	{ 3, "0 E", (int64_t)1500*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
	{ 3, "2 E", (int64_t)1500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },

	/* tests 62-85 test specific scale factors with 1024 divisor */
	{ 3, "0 K", (int64_t)0L, 0, 1 },
	{ 3, "1 K", (int64_t)512L, 0, 1 },
	{ 3, "0 M", (int64_t)512L, 0, 2 },
	{ 3, "1 M", (int64_t)512*1024L, 0, 2 },
	{ 3, "0 G", (int64_t)512*1024L, 0, 3 },
	{ 3, "1 G", (int64_t)512*1024*1024L, 0, 3 },
	{ 3, "0 T", (int64_t)512*1024*1024L, 0, 4 },
	{ 3, "1 T", (int64_t)512*1024*1024*1024L, 0, 4 },
	{ 3, "0 P", (int64_t)512*1024*1024*1024L, 0, 5 },
	{ 3, "1 P", (int64_t)512*1024*1024*1024*1024L, 0, 5 },
	{ 3, "0 E", (int64_t)512*1024*1024*1024*1024L, 0, 6 },
	{ 3, "1 E", (int64_t)512*1024*1024*1024*1024*1024L, 0, 6 },
	{ 3, "0 K", (int64_t)1L, 0, 1 },
	{ 3, "2 K", (int64_t)1536L, 0, 1 },
	{ 3, "0 M", (int64_t)1536L, 0, 2 },
	{ 3, "2 M", (int64_t)1536*1024L, 0, 2 },
	{ 3, "0 G", (int64_t)1536*1024L, 0, 3 },
	{ 3, "2 G", (int64_t)1536*1024*1024L, 0, 3 },
	{ 3, "0 T", (int64_t)1536*1024*1024L, 0, 4 },
	{ 3, "2 T", (int64_t)1536*1024*1024*1024L, 0, 4 },
	{ 3, "0 P", (int64_t)1536*1024*1024*1024L, 0, 5 },
	{ 3, "2 P", (int64_t)1536*1024*1024*1024*1024L, 0, 5 },
	{ 3, "0 E", (int64_t)1536*1024*1024*1024*1024L, 0, 6 },
	{ 3, "2 E", (int64_t)1536*1024*1024*1024*1024*1024L, 0, 6 },

	/* tests 86-99 test invalid specific scale values of < 0 or >= 7 with
	and without HN_DIVISOR_1000 set */
	/*  all should return errors with new code; with old, the latter 3
	are instead processed as if having AUTOSCALE and/or GETSCALE set */
	{ -1, "", (int64_t)1L, 0, 7 },
	{ -1, "", (int64_t)1L, HN_DIVISOR_1000, 7 },
	{ -1, "", (int64_t)1L, 0, 1000 },
	{ -1, "", (int64_t)1L, HN_DIVISOR_1000, 1000 },
	{ -1, "", (int64_t)0L, 0, 1000*1000 },
	{ -1, "", (int64_t)0L, HN_DIVISOR_1000, 1000*1000 },
	{ -1, "", (int64_t)0L, 0, INT_MAX },
	{ -1, "", (int64_t)0L, HN_DIVISOR_1000, INT_MAX },

	/* Negative scale values are not handled well
	 by the existing library routine - should report as error */
	/*  all should return errors with new code, fail assertion with old */

	{ -1, "", (int64_t)1L, 0, -1 },
	{ -1, "", (int64_t)1L, HN_DIVISOR_1000, -1 },
	{ -1, "", (int64_t)1L, 0, -1000 },
	{ -1, "", (int64_t)1L, HN_DIVISOR_1000, -1000 },

	/* __INT_MIN doesn't print properly, skipped. */

	{ -1, "", (int64_t)1L, 0, -__INT_MAX },
	{ -1, "", (int64_t)1L, HN_DIVISOR_1000, -__INT_MAX },


	/* tests for scale == 0, without autoscale */
	/* tests 100-114 test scale 0 with 1000 divisor - print first N digits */
	{ 2, "0 ", (int64_t)0L, HN_DIVISOR_1000, 0 },
	{ 2, "1 ", (int64_t)1L, HN_DIVISOR_1000, 0 },
	{ 3, "10 ", (int64_t)10L, HN_DIVISOR_1000, 0 },
	{ 3, "0 M", (int64_t)150L, HN_DIVISOR_1000, HN_NOSPACE },
	{ 3, "0 M", (int64_t)500L, HN_DIVISOR_1000, HN_NOSPACE },
	{ 3, "0 M", (int64_t)999L, HN_DIVISOR_1000, HN_NOSPACE },
	{ 4, "150", (int64_t)150L, HN_DIVISOR_1000, 0 },
	{ 4, "500", (int64_t)500L, HN_DIVISOR_1000, 0 },
	{ 4, "999", (int64_t)999L, HN_DIVISOR_1000, 0 },
	{ 5, "100", (int64_t)1000L, HN_DIVISOR_1000, 0 },
	{ 5, "150", (int64_t)1500L, HN_DIVISOR_1000, 0 },
	{ 7, "500", (int64_t)500*1000L, HN_DIVISOR_1000, 0 },
	{ 8, "150", (int64_t)1500*1000L, HN_DIVISOR_1000, 0 },
	{ 10, "500", (int64_t)500*1000*1000L, HN_DIVISOR_1000, 0 },
	{ 11, "150", (int64_t)1500*1000*1000L, HN_DIVISOR_1000, 0 },

	/* tests 115-126 test scale 0 with 1024 divisor - print first N digits */
	{ 2, "0 ", (int64_t)0L, 0, 0 },
	{ 2, "1 ", (int64_t)1L, 0, 0 },
	{ 3, "10 ", (int64_t)10L, 0, 0 },
	{ 4, "150", (int64_t)150L, 0, 0 },
	{ 4, "500", (int64_t)500L, 0, 0 },
	{ 4, "999", (int64_t)999L, 0, 0 },
	{ 5, "100", (int64_t)1000L, 0, 0 },
	{ 5, "150", (int64_t)1500L, 0, 0 },
	{ 7, "500", (int64_t)500*1000L, 0, 0 },
	{ 8, "150", (int64_t)1500*1000L, 0, 0 },
	{ 10, "500", (int64_t)500*1000*1000L, 0, 0 },
	{ 11, "150", (int64_t)1500*1000*1000L, 0, 0 },

	/* Test boundary cases for very large positive/negative number formatting */
	/* Explicit scale, divisor 1024 */

	/* XXX = requires length 5 (buflen 6) for some cases*/
	/* KLUDGE - test loop below will bump length 5 up to 5 */
	{ 3, "8 E",   INT64_MAX, 0, 6 },
	{ 4, "-8 E", -INT64_MAX, 0, 6 },
	{ 3, "0 E", (int64_t)92*1024*1024*1024*1024*1024L, 0, 6 },
	{ 3, "0 E", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 6 },
	{ 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, 0, 6 },
	{ 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 6 },
	{ 3, "0 E", (int64_t)81*1024*1024*1024*1024*1024L, 0, 6 },
	{ 3, "0 E", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 6 },
	{ 4, "92 P", (int64_t)92*1024*1024*1024*1024*1024L, 0, 5 },
	{ 5, "-92 P", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 5 },
	{ 4, "82 P", (int64_t)82*1024*1024*1024*1024*1024L, 0, 5 },
	{ 5, "-82 P", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 5 },
	{ 4, "81 P", (int64_t)81*1024*1024*1024*1024*1024L, 0, 5 },
	{ 5, "-81 P", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 5 },

	/* Explicit scale, divisor 1000 */
	{ 3, "9 E",   INT64_MAX, HN_DIVISOR_1000, 6 },
	{ 4, "-9 E", -INT64_MAX, HN_DIVISOR_1000,  6 },
	{ 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  6 },
	{ 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  6 },
	{ 3, "0 E", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  6 },
	{ 3, "0 E", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  6 },
	{ 4, "92 P", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  5 },
	{ 5, "-92 P", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  5 },
	{ 4, "91 P", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  5 },
	{ 5, "-91 P", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  5 },

	/* Autoscale, divisor 1024 */
	{ 3, "8 E",   INT64_MAX, 0, HN_AUTOSCALE },
	{ 4, "-8 E", -INT64_MAX, 0, HN_AUTOSCALE },
	{ 4, "92 P", (int64_t)92*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
	{ 5, "-92 P", -(int64_t)92*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
	{ 4, "82 P", (int64_t)82*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
	{ 5, "-82 P", -(int64_t)82*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
	{ 4, "81 P", (int64_t)81*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
	{ 5, "-81 P", -(int64_t)81*1024*1024*1024*1024*1024L, 0, HN_AUTOSCALE },
	/* Autoscale, divisor 1000 */
	{ 3, "9 E",   INT64_MAX, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 4, "-9 E", -INT64_MAX, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 4, "92 P", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "-92 P", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 4, "91 P", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "-91 P", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000, HN_AUTOSCALE },

	/* 0 scale, divisor 1024 */
	{ 12, "skdj",  INT64_MAX, 0, 0 },
	{ 21, "-9223", -INT64_MAX, 0, 0 },
	{ 19, "10358", (int64_t)92*1024*1024*1024*1024*1024L, 0, 0 },
	{ 20, "-1035", -(int64_t)92*1024*1024*1024*1024*1024L, 0, 0 },
	{ 18, "92323", (int64_t)82*1024*1024*1024*1024*1024L, 0, 0 },
	{ 19, "-9232", -(int64_t)82*1024*1024*1024*1024*1024L, 0, 0 },
	{ 18, "91197", (int64_t)81*1024*1024*1024*1024*1024L, 0, 0 },
	{ 19, "-9119", -(int64_t)81*1024*1024*1024*1024*1024L, 0, 0 },

	/* 0 scale, divisor 1000 */
	/* XXX - why does this fail? */
	{ -1, "", INT64_MAX, HN_DIVISOR_1000, 0 },
	{ 21, "-9223", -INT64_MAX, HN_DIVISOR_1000,  0 },
	{ 19, "10358", (int64_t)92*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
	{ 20, "-1035", -(int64_t)92*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
	{ 18, "92323", (int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
	{ 19, "-9232", -(int64_t)82*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
	/* Expected to pass */
	{ 18, "91197", (int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },
	{ 19, "-9119", -(int64_t)81*1024*1024*1024*1024*1024L, HN_DIVISOR_1000,  0 },



	/* Need to implement tests for GETSCALE */
/*	{ ?, "", (int64_t)0L, HN_DIVISOR_1000, HN_GETSCALE },
	...
*/
        /* Tests for HN_DECIMAL */
        /* Positive, Autoscale */
	{ 5, "500 k", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "994 k", (int64_t)994*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "995 k", (int64_t)995*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "999 k", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "1.0 M", (int64_t)1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "1.5 M", (int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "1.9 M", (int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "2.0 M", (int64_t)1950*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "9.9 M", (int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 4, "10 M", (int64_t)9950*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "500 M", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "994 M", (int64_t)994*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "995 M", (int64_t)995*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "999 M", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },

	{ 5, "500 K", (int64_t)500*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "994 K", (int64_t)994*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "995 K", (int64_t)995*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "999 K", (int64_t)999*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "1.0 M", (int64_t)1000*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "1.0 M", (int64_t)1018*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "1.0 M", (int64_t)1019*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "1.5 M", (int64_t)1536*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "1.9 M", (int64_t)1996*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "2.0 M", (int64_t)1997*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "2.0 M", (int64_t)2047*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "2.0 M", (int64_t)2048*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "2.0 M", (int64_t)2099*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "2.1 M", (int64_t)2100*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "9.9 M", (int64_t)10188*1024L, HN_DECIMAL, HN_AUTOSCALE },
	/* XXX - shouldn't the following two be "10. M"? */
	{ 4, "10 M", (int64_t)10189*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 4, "10 M", (int64_t)10240*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "500 M", (int64_t)500*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "994 M", (int64_t)994*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "995 M", (int64_t)995*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "999 M", (int64_t)999*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "1.0 G", (int64_t)1000*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "1.0 G", (int64_t)1023*1024*1024L, HN_DECIMAL, HN_AUTOSCALE },

        /* Negative, Autoscale - should pass */
	{ 6, "-1.5 ", -(int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 6, "-1.9 ", -(int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 6, "-9.9 ", -(int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },

	{ 6, "-1.5 ", -(int64_t)1536*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 6, "-1.9 ", -(int64_t)1949*1024L, HN_DECIMAL, HN_AUTOSCALE },
	{ 6, "-9.7 ", -(int64_t)9949*1024L, HN_DECIMAL, HN_AUTOSCALE },

	/* Positive/negative, at maximum scale */
	{ 5, "500 P", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "1.9 E", (int64_t)1949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "8.9 E", (int64_t)8949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 5, "9.2 E", INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
        /* Negatives work with latest rev only: */
	{ 6, "-9.2 ", -INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },
	{ 6, "-8.9 ", -(int64_t)8949*1000*1000*1000*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, HN_AUTOSCALE },

	{ 5, "8.0 E",   INT64_MAX, HN_DECIMAL, HN_AUTOSCALE },
	{ 5, "7.9 E",   INT64_MAX-(int64_t)100*1024*1024*1024*1024*1024LL, HN_DECIMAL, HN_AUTOSCALE },
	{ 6, "-8.0 ", -INT64_MAX, HN_DECIMAL, HN_AUTOSCALE },
	{ 6, "-7.9 ",   -INT64_MAX+(int64_t)100*1024*1024*1024*1024*1024LL, HN_DECIMAL, HN_AUTOSCALE },

	/* Positive, Fixed scales */
	{ 5, "500 k", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 },
	{ 5, "0.5 M", (int64_t)500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 5, "949 k", (int64_t)949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 },
	{ 5, "0.9 M", (int64_t)949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 5, "950 k", (int64_t)950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 },
	{ 5, "1.0 M", (int64_t)950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 5, "999 k", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, 1 },
	{ 5, "1.0 M", (int64_t)999*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 5, "1.5 M", (int64_t)1500*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 5, "1.9 M", (int64_t)1949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 5, "2.0 M", (int64_t)1950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 5, "9.9 M", (int64_t)9949*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 4, "10 M", (int64_t)9950*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 5, "500 M", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 5, "0.5 G", (int64_t)500*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 3 },
	{ 5, "999 M", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 2 },
	{ 5, "1.0 G", (int64_t)999*1000*1000L, HN_DECIMAL|HN_DIVISOR_1000, 3 },
	/* Positive/negative, at maximum scale */
	{ 5, "500 P", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 5 },
	{ 5, "1.0 E", (int64_t)500*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
	{ 5, "1.9 E", (int64_t)1949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
	{ 5, "8.9 E", (int64_t)8949*1000*1000*1000*1000*1000L, HN_DIVISOR_1000, 6 },
	{ 5, "9.2 E", INT64_MAX, HN_DECIMAL|HN_DIVISOR_1000, 6 },

	/* HN_DECIMAL + binary + fixed scale cases not completed */
	{ 5, "512 K", (int64_t)512*1024L, HN_DECIMAL, 1 },
	{ 5, "0.5 M", (int64_t)512*1024L, HN_DECIMAL, 2 },

	/* Negative, Fixed scales */
        /* Not yet added, but should work with latest rev */

};


/* Command line options usage */
static void
usage(char * progname) {
	printf("%s: tests libutil humanize_number function\n", progname);
	printf("Usage: %s [-nE] [-l num] [-v]\n\n", progname);
	printf("Options:\n");
	printf("\t-l num\tSet max length for result; buflen = num + 1\n");
	printf("\t\t  (NOTE: does not change expected result strings.)\n");
	printf("\t-n\tInclude negative scale tests, which cause older libutil\n");
	printf("\t\t  version of function to coredump with assertion failure\n");
	printf("\t-E\tInclude numbers > 1/2 Exa[byte] which currently fail\n");
	printf("\t-v\tVerbose - always print summary results\n");
	printf("\t-h, -?\tShow options\n");
}

/* Parse command line options */
static void
read_options(int argc, char * const argv[], size_t *bufLength,
    int *includeNegativeScale, int *includeExabytes, int *verbose) {
	int ch;
	size_t temp;

	while ((ch = getopt(argc, argv, "nEh?vl:")) != -1) {
		switch (ch) {
			default:
				usage(argv[0]);
				exit(1);
				break;	/* UNREACHABLE */
			case 'h' :
			case '?' :
				usage(argv[0]);
				exit(0);
				break;	/* UNREACHABLE */
			case 'l' :
				sscanf(optarg, "%zu", &temp);
				*bufLength = temp + 1;
				break;
			case 'n' :
				*includeNegativeScale = 1;
				break;
			case 'E' :
				*includeExabytes = 1;
				break;
			case 'v' :
				*verbose = 1;
				break;
		}
	}
}

static struct {
	int value;
	const char *name;
 } flags[] = {
	{ HN_AUTOSCALE, "HN_AUTOSCALE" },
	{ HN_GETSCALE, "HN_GETSCALE" },
	{ HN_DIVISOR_1000, "HN_DIVISOR_1000"},
	{ HN_B, "HN_B"},
	{ HN_DECIMAL, "HN_DECIMAL"},
};

static const char *separator = "|";

/* Format flags parameter for meaningful display */
static char *
str_flags(int hn_flags, char *noFlags) {
	size_t i;
	char * result;

	result = malloc(MAX_STR_FLAGS_RESULT);
	result[0] = '\0';

	for (i = 0; i < sizeof flags / sizeof *flags; i++) {
		if (hn_flags & flags[i].value) {
			if (*result != 0)
				strlcat(result, separator,
				    MAX_STR_FLAGS_RESULT);
			strlcat(result, flags[i].name, MAX_STR_FLAGS_RESULT);
		}
	}

	if (strlen(result) == 0)
		strlcat(result, noFlags, MAX_STR_FLAGS_RESULT);
	return result;
}


/* Format scale parameter for meaningful display */
static char *
str_scale(int scale) {
	char *result;

	if (scale == HN_AUTOSCALE || scale == HN_GETSCALE)
		return str_flags(scale, "");

	result = malloc(MAX_INT_STR_DIGITS);
	result[0] = '\0';
	snprintf(result, MAX_INT_STR_DIGITS, "%d", scale);
	return result;
}

static void
testskipped(size_t i)
{

	printf("ok %lu # skip - not turned on\n", i);
}

int
main(int argc, char * const argv[])
{
	char *buf;
	char *flag_str, *scale_str;
	size_t i;
	size_t errcnt, tested, skipped;
	int r;
	size_t buflen;
	int includeNegScale;
	int includeExabyteTests;
	int verbose;

	buflen = 4;
	includeNegScale = 0;
	includeExabyteTests = 0;
	verbose = 0;

	read_options(argc, argv, &buflen, &includeNegScale,
	    &includeExabyteTests, &verbose);

	buf = malloc(buflen);
	errcnt = 0;
	tested = 0;
	skipped = 0;

	if (buflen != 4)
		printf("Warning: buffer size %zu != 4, expect some results to differ.\n", buflen);

	printf("1..%lu\n", sizeof test_args / sizeof *test_args);
	for (i = 0; i < sizeof test_args / sizeof *test_args; i++) {
		/* KLUDGE */
		if (test_args[i].num == INT64_MAX && buflen == 4) {
		        /* Start final tests which require buffer of 6 */
		        free(buf);
		        buflen = 6;
                        buf = malloc(buflen);
                        if (verbose)
                                printf("Buffer length increased to %zu\n",
				    buflen);
		}

		if (test_args[i].scale < 0 && ! includeNegScale) {
			skipped++;
			testskipped(i);
			continue;
		}
		if (test_args[i].num >= halfExabyte && ! includeExabyteTests) {
			skipped++;
			testskipped(i);
			continue;
		}

		r = humanize_number(buf, buflen, test_args[i].num, "",
		    test_args[i].scale, test_args[i].flags);
		flag_str = str_flags(test_args[i].flags, "[no flags]");
		scale_str = str_scale(test_args[i].scale);

		if (r != test_args[i].retval) {
			if (verbose)
				printf("wrong return value on index %lu, buflen: %zu, got: %d + \"%s\", expected %d + \"%s\"; num = %" PRId64 ", scale = %s, flags= %s.\n",
				    i, buflen, r, buf, test_args[i].retval,
				    test_args[i].res, test_args[i].num,
				    scale_str, flag_str);
			else
				printf("not ok %lu # return %d != %d\n", i, r,
				    test_args[i].retval);
			errcnt++;
		} else if (strcmp(buf, test_args[i].res) != 0) {
			if (verbose)
				printf("result mismatch on index %lu, got: \"%s\", expected \"%s\"; num = %" PRId64 ", scale = %s, flags= %s.\n",
				    i, buf, test_args[i].res, test_args[i].num,
				    scale_str, flag_str);
			else
				printf("not ok %lu # buf \"%s\" != \"%s\"\n", i,
				    buf, test_args[i].res);
			errcnt++;
		} else {
			if (verbose)
				printf("successful result on index %lu, returned %d, got: \"%s\"; num = %" PRId64 ", scale = %s, flags= %s.\n",
				    i, r, buf, test_args[i].num, scale_str,
				    flag_str);
			else
				printf("ok %lu\n", i);
		}
		tested++;
	}

	if (verbose)
		printf("total errors: %lu/%lu tests, %lu skipped\n", errcnt,
		    tested, skipped);

	if (errcnt)
		return 1;

	return 0;
}
OpenPOWER on IntegriCloud