summaryrefslogtreecommitdiffstats
path: root/test/bittests.c
blob: 13ad749c766add0a8dd6206deda08ff53e6d4372 (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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Programmer:  Robb Matzke
 *              Tuesday, June 16, 1998
 *
 * Purpose:	Tests functions in H5Tbit.c
 */
#include "h5test.h"

#define H5T_FRIEND /*suppress error about including H5Tpkg	  */
#include "H5Tpkg.h"

#define NTESTS 100000

/*-------------------------------------------------------------------------
 * Function:	test_find
 *
 * Purpose:	Test bit find operations.  This is just the basic stuff; more
 *		rigorous testing will be performed by the other test
 *		functions.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Robb Matzke
 *              Tuesday, June 16, 1998
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_find(void)
{
    uint8_t v1[8];
    int     i;
    ssize_t n;

    TESTING("bit search operations");

    /* The zero length buffer */
    HDmemset(v1, 0xaa, sizeof v1);
    n = H5T__bit_find(v1, (size_t)0, (size_t)0, H5T_BIT_LSB, TRUE);
    if (-1 != n) {
        H5_FAILED();
        HDputs("    Zero length test failed (lsb)!");
        goto failed;
    }
    n = H5T__bit_find(v1, (size_t)0, (size_t)0, H5T_BIT_MSB, TRUE);
    if (-1 != n) {
        H5_FAILED();
        HDputs("    Zero length test failed (msb)!");
        goto failed;
    }

    /* The zero buffer */
    HDmemset(v1, 0, sizeof v1);
    n = H5T__bit_find(v1, (size_t)0, 8 * sizeof(v1), H5T_BIT_LSB, TRUE);
    if (-1 != n) {
        H5_FAILED();
        HDputs("    Zero buffer test failed (lsb)!");
        goto failed;
    }
    n = H5T__bit_find(v1, (size_t)0, 8 * sizeof(v1), H5T_BIT_MSB, TRUE);
    if (-1 != n) {
        H5_FAILED();
        HDputs("    Zero buffer test failed (msb)!");
        goto failed;
    }

    /* Try all combinations of one byte */
    for (i = 0; i < 8 * (int)sizeof(v1); i++) {
        HDmemset(v1, 0, sizeof v1);
        v1[i / 8] = (uint8_t)(1 << (i % 8));
        n         = H5T__bit_find(v1, (size_t)0, 8 * sizeof(v1), H5T_BIT_LSB, TRUE);
        if ((ssize_t)i != n) {
            H5_FAILED();
            printf("    Test for set bit %d failed (lsb)!\n", i);
            goto failed;
        }
        n = H5T__bit_find(v1, (size_t)0, 8 * sizeof(v1), H5T_BIT_MSB, TRUE);
        if ((ssize_t)i != n) {
            H5_FAILED();
            printf("    Test for set bit %d failed (msb)!\n", i);
            goto failed;
        }
    }

    /* The one buffer */
    HDmemset(v1, 0xff, sizeof v1);
    n = H5T__bit_find(v1, (size_t)0, 8 * sizeof(v1), H5T_BIT_LSB, FALSE);
    if (-1 != n) {
        H5_FAILED();
        HDputs("    One buffer test failed (lsb)!");
        goto failed;
    }
    n = H5T__bit_find(v1, (size_t)0, 8 * sizeof(v1), H5T_BIT_MSB, FALSE);
    if (-1 != n) {
        H5_FAILED();
        HDputs("    One buffer test failed (msb)!");
        goto failed;
    }

    /* Try all combinations of one byte */
    for (i = 0; i < 8 * (int)sizeof(v1); i++) {
        HDmemset(v1, 0xff, sizeof v1);
        v1[i / 8] &= (uint8_t) ~(1 << (i % 8));
        n = H5T__bit_find(v1, (size_t)0, 8 * sizeof(v1), H5T_BIT_LSB, FALSE);
        if ((ssize_t)i != n) {
            H5_FAILED();
            printf("    Test for clear bit %d failed (lsb)!\n", i);
            goto failed;
        }
        n = H5T__bit_find(v1, (size_t)0, 8 * sizeof(v1), H5T_BIT_MSB, FALSE);
        if ((ssize_t)i != n) {
            H5_FAILED();
            printf("    Test for clear bit %d failed (lsb)!\n", i);
            goto failed;
        }
    }

    PASSED();
    return 0;

failed:
    printf("    v = 0x");
    for (i = 0; i < (int)sizeof(v1); i++)
        printf("%02x", v1[i]);
    printf("\n");
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:	test_copy
 *
 * Purpose:	Test bit copy operations.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Robb Matzke
 *              Tuesday, June 16, 1998
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_copy(void)
{
    uint8_t v1[8], v2[8];
    size_t  s_offset, d_offset, size;
    int     i, j;
    ssize_t n;

    TESTING("bit copy operations");

    for (i = 0; i < NTESTS; i++) {
        s_offset = (size_t)HDrand() % (8 * sizeof v1);
        d_offset = (size_t)HDrand() % (8 * sizeof v2);
        size     = (unsigned)HDrand() % MIN(8 * sizeof(v1), 8 * sizeof(v2));
        size     = MIN3(size, 8 * sizeof(v1) - s_offset, 8 * sizeof(v2) - d_offset);
        HDmemset(v1, 0xff, sizeof v1);
        HDmemset(v2, 0x00, sizeof v2);

        /* Copy some bits to v2 and make sure something was copied */
        H5T__bit_copy(v2, d_offset, v1, s_offset, size);
        for (j = 0; j < (int)sizeof(v2); j++)
            if (v2[j])
                break;
        if (size > 0 && j >= (int)sizeof(v2)) {
            H5_FAILED();
            HDputs("    Unabled to find copied region in destination");
            goto failed;
        }
        if (0 == size && j < (int)sizeof(v2)) {
            H5_FAILED();
            HDputs("    Found copied bits when we shouldn't have");
            goto failed;
        }

        /* Look for the zeros and ones */
        n = H5T__bit_find(v2, (size_t)0, 8 * sizeof(v2), H5T_BIT_LSB, 1);
        if (size > 0 && n != (ssize_t)d_offset) {
            H5_FAILED();
            printf("    Unable to find first copied bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (0 == size && n >= 0) {
            H5_FAILED();
            HDputs("    Found copied bits and shouldn't have!");
            goto failed;
        }
        n = H5T__bit_find(v2, d_offset, 8 * sizeof(v2) - d_offset, H5T_BIT_LSB, 0);
        if (d_offset + size < 8 * sizeof(v2) && n != (ssize_t)size) {
            H5_FAILED();
            printf("    Unable to find last copied bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (d_offset + size == 8 * sizeof(v2) && n >= 0) {
            H5_FAILED();
            HDputs("    High-order zeros are present and shouldn't be!");
            goto failed;
        }

        /*
         * Look for zeros and ones in reverse order.  This is only to test
         * that reverse searches work as expected.
         */
        n = H5T__bit_find(v2, (size_t)0, 8 * sizeof(v2), H5T_BIT_MSB, 1);
        if (size > 0 && (size_t)(n + 1) != d_offset + size) {
            H5_FAILED();
            printf("    Unable to find last copied bit in destination "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (0 == size && n >= 0) {
            H5_FAILED();
            HDputs("    Found copied bits but shouldn't have (reverse)!");
            goto failed;
        }
        n = H5T__bit_find(v2, (size_t)0, d_offset + size, H5T_BIT_MSB, 0);
        if (d_offset > 0 && n + 1 != (ssize_t)d_offset) {
            H5_FAILED();
            printf("    Unable to find beginning of copied data "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (0 == d_offset && n >= 0) {
            H5_FAILED();
            HDputs("    Found leading original data but shouldn't have!");
            goto failed;
        }
    }

    PASSED();
    return 0;

failed:
    printf("    i=%d, s_offset=%lu, d_offset=%lu, size=%lu\n", i, (unsigned long)s_offset,
           (unsigned long)d_offset, (unsigned long)size);
    printf("    s = 0x");
    for (j = sizeof(v1) - 1; j >= 0; --j)
        printf("%02x", v1[j]);
    printf("\n    d = 0x");
    for (j = sizeof(v2) - 1; j >= 0; --j)
        printf("%02x", v2[j]);
    printf("\n");
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:	test_shift
 *
 * Purpose:	Test bit shifting operations.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *              Monday, April 12, 2004
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_shift(void)
{
    uint8_t vector[8];
    size_t  offset, size;
    int     i, j;
    ssize_t shift_dist, n;

    TESTING("bit shift operations");

    for (i = 0; i < NTESTS; i++) {
        offset = (size_t)HDrand() % (8 * sizeof vector);
        size   = (size_t)HDrand() % (8 * sizeof(vector) - offset);
        /* Don't want size to be 0 */
        if (size == 0)
            continue;
        shift_dist = (ssize_t)((size_t)HDrand() % size);

        /*-------- LEFT-shift some bits and make sure something was shifted --------*/
        HDmemset(vector, 0x00, sizeof vector);
        H5T__bit_set(vector, offset, size, 1);

        H5T__bit_shift(vector, shift_dist, offset, size);

        /* Look for the ones */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_LSB, 1);
        if (n != (ssize_t)offset + shift_dist) {
            H5_FAILED();
            printf("    Unable to find first bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }

        /*
         * Look for zeros and ones in reverse order.  This is only to test
         * that reverse searches work as expected.
         */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_MSB, 1);
        if (n != (ssize_t)(offset + size - 1)) {
            H5_FAILED();
            printf("    Unable to find last bit in destination "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }

        /*-------- RIGHT-shift some bits and make sure something was shifted --------*/
        HDmemset(vector, 0x00, sizeof vector);
        H5T__bit_set(vector, offset, size, 1);

        H5T__bit_shift(vector, -shift_dist, offset, size);

        /* Look for the ones */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_LSB, 1);
        if ((size_t)n != offset) {
            H5_FAILED();
            printf("    Unable to find first bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }

        /*
         * Look for zeros and ones in reverse order.  This is only to test
         * that reverse searches work as expected.
         */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_MSB, 1);
        if (n != (ssize_t)(offset + size) - shift_dist - 1) {
            H5_FAILED();
            printf("    Unable to find last bit in destination "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }

        /*-------- Shift the bits out of sight --------*/
        /* A sequence 111111 will be 000000 if shift_dist=6 */

        /* Randomly decide shift direction */
        if (size % 2 == 0)
            shift_dist = (ssize_t)size;
        else
            shift_dist = -((ssize_t)size);

        HDmemset(vector, 0x00, sizeof vector);
        H5T__bit_set(vector, offset, size, 1);

        H5T__bit_shift(vector, shift_dist, offset, size);

        /* Supposed to fail to find any ones */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_LSB, 1);
        if (n >= 0) {
            H5_FAILED();
            printf("    Unable to verify all bits are zero in destination(LSB) "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }

        /* Look from the other direction */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_MSB, 1);
        if (n >= 0) {
            H5_FAILED();
            printf("    Unable to verify all bits are zero in destination(MSB) "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }
    }

    PASSED();
    return 0;

failed:
    printf("    i=%d, offset=%lu, size=%lu, shift_dist=%lu\n", i, (unsigned long)offset, (unsigned long)size,
           (unsigned long)shift_dist);
    for (j = sizeof(vector) - 1; j >= 0; --j)
        printf("%02x", vector[j]);
    printf("\n");
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:	test_increment
 *
 * Purpose:	Test operation to increment bit vector by 1.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *              Monday, April 12, 2004
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_increment(void)
{
    uint8_t vector[8];
    size_t  offset, size;
    int     i, j;
    ssize_t n;

    TESTING("bit increment operations");

    for (i = 0; i < NTESTS; i++) {
        offset = (size_t)HDrand() % (8 * sizeof vector);
        size   = (size_t)HDrand() % (8 * sizeof(vector) - offset);
        /* Don't want size to be 0 */
        if (size == 0)
            continue;

        HDmemset(vector, 0x00, sizeof vector);
        if (size > 1) /* if size=6, make a sequence like 011111 */
            H5T__bit_set(vector, offset, size - 1, 1);
        else /* if size=1, just set this one bit to 1 */
            H5T__bit_set(vector, offset, size, 1);

        /* Increment the sequence by one */
        H5T__bit_inc(vector, offset, size);

        /* Look for the one */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_LSB, 1);
        if (size != 1 && (size_t)n != offset + size - 1) {
            H5_FAILED();
            printf("    Unable to find first bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (size == 1 && n >= 0) {
            H5_FAILED();
            printf("    Unable to verify all-zero bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }

        /*
         * Look for one in reverse order.  This is only to test
         * that reverse searches work as expected.
         */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_MSB, 1);
        if (size != 1 && n != (ssize_t)(offset + size - 1)) {
            H5_FAILED();
            printf("    Unable to find last bit in destination "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (size == 1 && n >= 0) {
            H5_FAILED();
            printf("    Unable to verify all-zero bit in destination "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }
    }

    PASSED();
    return 0;

failed:
    printf("    i=%d, offset=%lu, size=%lu\n", i, (unsigned long)offset, (unsigned long)size);
    for (j = sizeof(vector) - 1; j >= 0; --j)
        printf("%02x", vector[j]);
    printf("\n");
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:	test_decrement
 *
 * Purpose:	Test operation to decrement bit vector by 1.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *              Monday, April 12, 2004
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_decrement(void)
{
    uint8_t vector[8];
    size_t  offset, size;
    int     i, j;
    ssize_t n;

    TESTING("bit decrement operations");

    for (i = 0; i < NTESTS; i++) {
        offset = (size_t)HDrand() % (8 * sizeof vector);
        size   = (size_t)HDrand() % (8 * sizeof(vector) - offset);
        /* Don't want size to be 0 */
        if (size == 0)
            continue;

        /* All-zero sequence will become 111111(size=6) after decrement */
        HDmemset(vector, 0x00, sizeof vector);

        /* decrement the sequence by one */
        H5T__bit_dec(vector, offset, size);

        /* Look for the ones */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_LSB, 1);
        if ((size_t)n != offset) {
            H5_FAILED();
            printf("    Unable to find first bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }

        /*
         * Look for zeros and ones in reverse order.  This is only to test
         * that reverse searches work as expected.
         */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_MSB, 1);
        if (n != (ssize_t)(offset + size - 1)) {
            H5_FAILED();
            printf("    Unable to find last bit in destination "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }
    }

    PASSED();
    return 0;

failed:
    printf("    i=%d, offset=%lu, size=%lu\n", i, (unsigned long)offset, (unsigned long)size);
    for (j = sizeof(vector) - 1; j >= 0; --j)
        printf("%02x", vector[j]);
    printf("\n");
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:	test_negate
 *
 * Purpose:	Test operation to negate bit vector.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *              Monday, April 12, 2004
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_negate(void)
{
    uint8_t vector[8];
    size_t  offset, size;
    int     i, j;
    ssize_t n;

    TESTING("bit negate operations");

    for (i = 0; i < NTESTS; i++) {
        offset = (size_t)HDrand() % (8 * sizeof vector);
        size   = (size_t)HDrand() % (8 * sizeof(vector) - offset);
        /* Don't want size to be 0 */
        if (size == 0)
            continue;

        /* All-zero sequence will become 111111(size=6) after negating */
        HDmemset(vector, 0x00, sizeof vector);

        /* negate the sequence */
        H5T__bit_neg(vector, offset, size);

        /* Look for the ones */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_LSB, 1);
        if ((size_t)n != offset) {
            H5_FAILED();
            printf("    Unable to find first bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }

        /*
         * Look for zeros and ones in reverse order.  This is only to test
         * that reverse searches work as expected.
         */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_MSB, 1);
        if (n != (ssize_t)(offset + size - 1)) {
            H5_FAILED();
            printf("    Unable to find last bit in destination "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }

        /* All-one sequence will become 000000(size=6) after negating */
        HDmemset(vector, 0x00, sizeof vector);
        H5T__bit_set(vector, offset, size, 1);

        /* negate the sequence */
        H5T__bit_neg(vector, offset, size);

        /* Look for the ones */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_LSB, 1);
        if (n >= 0) {
            H5_FAILED();
            printf("    Unable to verify all-zero bits in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }

        /*
         * Look for ones in reverse order.  This is only to test
         * that reverse searches work as expected.
         */
        n = H5T__bit_find(vector, (size_t)0, 8 * sizeof(vector), H5T_BIT_MSB, 1);
        if (n >= 0) {
            H5_FAILED();
            printf("    Unable to verify all-zero bits in destination "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }
    }

    PASSED();
    return 0;

failed:
    printf("    i=%d, offset=%lu, size=%lu\n", i, (unsigned long)offset, (unsigned long)size);
    for (j = sizeof(vector) - 1; j >= 0; --j)
        printf("%02x", vector[j]);
    printf("\n");
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:	test_set
 *
 * Purpose:	Test bit set operations
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Robb Matzke
 *              Tuesday, June 16, 1998
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_set(void)
{
    uint8_t v2[8];
    size_t  d_offset, size;
    int     i, j;
    ssize_t n;

    TESTING("bit set operations");

    for (i = 0; i < NTESTS; i++) {
        d_offset = (size_t)HDrand() % (8 * sizeof v2);
        size     = (size_t)HDrand() % (8 * sizeof(v2));
        size     = MIN(size, 8 * sizeof(v2) - d_offset);
        HDmemset(v2, 0x00, sizeof v2);

        /* Set some bits in v2 */
        H5T__bit_set(v2, d_offset, size, TRUE);
        for (j = 0; j < (int)sizeof(v2); j++)
            if (v2[j])
                break;
        if (size > 0 && j >= (int)sizeof(v2)) {
            H5_FAILED();
            HDputs("    Unabled to find set region in buffer");
            goto failed;
        }
        if (0 == size && j < (int)sizeof(v2)) {
            H5_FAILED();
            HDputs("    Found set bits when we shouldn't have");
            goto failed;
        }

        /* Look for the zeros and ones */
        n = H5T__bit_find(v2, (size_t)0, 8 * sizeof(v2), H5T_BIT_LSB, 1);
        if (size > 0 && n != (ssize_t)d_offset) {
            H5_FAILED();
            printf("    Unable to find first set bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (0 == size && n >= 0) {
            H5_FAILED();
            HDputs("    Found set bits and shouldn't have!");
            goto failed;
        }
        n = H5T__bit_find(v2, d_offset, 8 * sizeof(v2) - d_offset, H5T_BIT_LSB, 0);
        if (d_offset + size < 8 * sizeof(v2) && n != (ssize_t)size) {
            H5_FAILED();
            printf("    Unable to find last set bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (d_offset + size == 8 * sizeof(v2) && n >= 0) {
            H5_FAILED();
            HDputs("    High-order zeros are present and shouldn't be!");
            goto failed;
        }

        /*
         * Look for zeros and ones in reverse order.  This is only to test
         * that reverse searches work as expected.
         */
        n = H5T__bit_find(v2, (size_t)0, 8 * sizeof(v2), H5T_BIT_MSB, 1);
        if (size > 0 && (size_t)(n + 1) != d_offset + size) {
            H5_FAILED();
            printf("    Unable to find last set bit in destination "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (0 == size && n >= 0) {
            H5_FAILED();
            HDputs("    Found set bits but shouldn't have (reverse)!");
            goto failed;
        }
        n = H5T__bit_find(v2, (size_t)0, d_offset + size, H5T_BIT_MSB, 0);
        if (d_offset > 0 && n + 1 != (ssize_t)d_offset) {
            H5_FAILED();
            printf("    Unable to find beginning of set bit region "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (0 == d_offset && n >= 0) {
            H5_FAILED();
            HDputs("    Found leading zeros but shouldn't have!");
            goto failed;
        }
    }

    PASSED();
    return 0;

failed:
    printf("    i=%d, d_offset=%lu, size=%lu\n", i, (unsigned long)d_offset, (unsigned long)size);
    printf("    d = 0x");
    for (j = sizeof(v2) - 1; j >= 0; --j)
        printf("%02x", v2[j]);
    printf("\n");
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:	test_clear
 *
 * Purpose:	Test bit clear operations
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Robb Matzke
 *              Tuesday, June 16, 1998
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_clear(void)
{
    uint8_t v2[8];
    size_t  d_offset, size;
    int     i, j;
    ssize_t n;

    TESTING("bit clear operations");

    for (i = 0; i < NTESTS; i++) {
        d_offset = (size_t)HDrand() % (8 * sizeof v2);
        size     = (size_t)HDrand() % (8 * sizeof(v2));
        size     = MIN(size, 8 * sizeof(v2) - d_offset);
        HDmemset(v2, 0xff, sizeof v2);

        /* Clear some bits in v2 */
        H5T__bit_set(v2, d_offset, size, FALSE);
        for (j = 0; j < (int)sizeof(v2); j++)
            if (0xff != v2[j])
                break;
        if (size > 0 && j >= (int)sizeof(v2)) {
            H5_FAILED();
            HDputs("    Unabled to find cleared region in buffer");
            goto failed;
        }
        if (0 == size && j < (int)sizeof(v2)) {
            H5_FAILED();
            HDputs("    Found cleared bits when we shouldn't have");
            goto failed;
        }

        /* Look for the zeros and ones */
        n = H5T__bit_find(v2, (size_t)0, 8 * sizeof(v2), H5T_BIT_LSB, 0);
        if (size > 0 && n != (ssize_t)d_offset) {
            H5_FAILED();
            printf("    Unable to find first cleared bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (0 == size && n >= 0) {
            H5_FAILED();
            HDputs("    Found cleared bits and shouldn't have!");
            goto failed;
        }
        n = H5T__bit_find(v2, d_offset, 8 * sizeof(v2) - d_offset, H5T_BIT_LSB, 1);
        if (d_offset + size < 8 * sizeof(v2) && n != (ssize_t)size) {
            H5_FAILED();
            printf("    Unable to find last cleared bit in destination "
                   "(n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (d_offset + size == 8 * sizeof(v2) && n >= 0) {
            H5_FAILED();
            HDputs("    High-order ones are present and shouldn't be!");
            goto failed;
        }

        /*
         * Look for zeros and ones in reverse order.  This is only to test
         * that reverse searches work as expected.
         */
        n = H5T__bit_find(v2, (size_t)0, 8 * sizeof(v2), H5T_BIT_MSB, 0);
        if (size > 0 && (size_t)(n + 1) != d_offset + size) {
            H5_FAILED();
            printf("    Unable to find last cleared bit in destination "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (0 == size && n >= 0) {
            H5_FAILED();
            HDputs("    Found cleared bits but shouldn't have (reverse)!");
            goto failed;
        }
        n = H5T__bit_find(v2, (size_t)0, d_offset + size, H5T_BIT_MSB, 1);
        if (d_offset > 0 && n + 1 != (ssize_t)d_offset) {
            H5_FAILED();
            printf("    Unable to find beginning of cleared bit region "
                   "(reverse, n=%d)\n",
                   (int)n);
            goto failed;
        }
        if (0 == d_offset && n >= 0) {
            H5_FAILED();
            HDputs("    Found leading ones but shouldn't have!");
            goto failed;
        }
    }

    PASSED();
    return 0;

failed:
    printf("    i=%d, d_offset=%lu, size=%lu\n", i, (unsigned long)d_offset, (unsigned long)size);
    printf("    d = 0x");
    for (j = sizeof(v2) - 1; j >= 0; --j)
        printf("%02x", v2[j]);
    printf("\n");
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:    main
 *
 * Purpose:
 *
 * Return:      EXIT_SUCCESS/EXIT_FAILURE
 *
 * Programmer:	Robb Matzke
 *              Tuesday, June 16, 1998
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    int nerrors = 0;

    /*
     * Open the library explicitly.
     */
    H5open();

    nerrors += test_find() < 0 ? 1 : 0;
    nerrors += test_set() < 0 ? 1 : 0;
    nerrors += test_clear() < 0 ? 1 : 0;
    nerrors += test_copy() < 0 ? 1 : 0;
    nerrors += test_shift() < 0 ? 1 : 0;
    nerrors += test_increment() < 0 ? 1 : 0;
    nerrors += test_decrement() < 0 ? 1 : 0;
    nerrors += test_negate() < 0 ? 1 : 0;

    if (nerrors) {
        printf("***** %u FAILURE%s! *****\n", nerrors, 1 == nerrors ? "" : "S");
        HDexit(EXIT_FAILURE);
    }
    printf("All bit tests passed.\n");

    H5close();

    HDexit(EXIT_SUCCESS);
} /* end main() */