rxing 0.4.11

A rust port of the zxing barcode library.
Documentation
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
/*
 * Copyright 2013 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use std::collections::HashMap;

use crate::{
    aztec::{
        aztec_detector_result::AztecDetectorRXingResult,
        decoder,
        encoder::HighLevelEncoder,
        shared_test_methods::{stripSpace, toBitArray, toBooleanArray},
    },
    common::CharacterSet,
    BarcodeFormat, EncodeHintType, EncodeHintValue, Point,
};

use super::{encoder::aztec_encoder, AztecWriter};

use crate::Writer;

use rand::Rng;

/**
 * Aztec 2D generator unit tests.
 *
 * @author Rustam Abdullaev
 * @author Frank Yellin
 */

const ISO_8859_1: CharacterSet = CharacterSet::ISO8859_1; //StandardCharsets.ISO_8859_1;
const UTF_8: CharacterSet = CharacterSet::UTF8; //StandardCharsets.UTF_8;
const ISO_8859_15: CharacterSet = CharacterSet::ISO8859_15; //Charset.forName("ISO-8859-15");
const WINDOWS_1252: CharacterSet = CharacterSet::Cp1252; //Charset.forName("Windows-1252");

// const DOTX: &str = "[^.X]";
// const SPACES: &str = "\\s+";
const NO_POINTS: [Point; 4] = [Point { x: 0.0, y: 0.0 }; 4];

// real life tests

#[test]
fn testEncode1() {
    testEncode(
        "This is an example Aztec symbol for Wikipedia.",
        true,
        3,
        r"X     X X       X     X X     X     X         
X         X     X X     X   X X   X X       X 
X X   X X X X X   X X X                 X     
X X                 X X   X       X X X X X X 
    X X X   X   X     X X X X         X X     
  X X X   X X X X   X     X   X     X X   X   
        X X X X X     X X X X   X   X     X   
X       X   X X X X X X X X X X X     X   X X 
X   X     X X X               X X X X   X X   
X     X X   X X   X X X X X   X X   X   X X X 
X   X         X   X       X   X X X X       X 
X       X     X   X   X   X   X   X X   X     
      X   X X X   X       X   X     X X X     
    X X X X X X   X X X X X   X X X X X X   X 
  X X   X   X X               X X X   X X X X 
  X   X       X X X X X X X X X X X X   X X   
  X X   X       X X X   X X X       X X       
  X               X   X X     X     X X X     
  X   X X X   X X   X   X X X X   X   X X X X 
    X   X   X X X   X   X   X X X X     X     
        X               X                 X   
        X X     X   X X   X   X   X       X X 
  X   X   X X       X   X         X X X     X 
",
    );
}

#[test]
fn testEncode2() {
    testEncode(
        "Aztec Code is a public domain 2D matrix barcode symbology of nominally square symbols built on a square grid with a distinctive square bullseye pattern at their center.",
        false,
        6,
        r"        X X     X X     X     X     X   X X X         X   X         X   X X       
  X       X X     X   X X   X X       X             X     X   X X   X           X 
  X   X X X     X   X   X X     X X X   X   X X               X X       X X     X 
X X X             X   X         X         X     X     X   X     X X       X   X   
X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X 
    X X   X   X   X X X               X       X       X X     X X   X X       X   
X X     X       X       X X X X   X   X X       X   X X   X       X X   X X   X   
  X       X   X     X X   X   X X   X X   X X X X X X   X X           X   X   X X 
X X   X X   X   X X X X   X X X X X X X X   X   X       X X   X X X X   X X X     
  X       X   X     X       X X     X X   X   X   X     X X   X X X   X     X X X 
  X   X X X   X X       X X X         X X           X   X   X   X X X   X X     X 
    X     X   X X     X X X X     X   X     X X X X   X X   X X   X X X     X   X 
X X X   X             X         X X X X X   X   X X   X   X   X X   X   X   X   X 
          X       X X X   X X     X   X           X   X X X X   X X               
  X     X X   X   X       X X X X X X X X X X X X X X X   X   X X   X   X X X     
    X X                 X   X                       X X   X       X         X X X 
        X   X X   X X X X X X   X X X X X X X X X   X     X X           X X X X   
          X X X   X     X   X   X               X   X X     X X X   X X           
X X     X     X   X   X   X X   X   X X X X X   X   X X X X X X X       X   X X X 
X X X X       X       X   X X   X   X       X   X   X     X X X     X X       X X 
X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X 
    X     X       X         X   X   X       X   X   X     X   X X                 
        X X     X X X X X   X   X   X X X X X   X   X X X     X X X X   X         
X     X   X   X         X   X   X               X   X X   X X   X X X     X   X   
  X   X X X   X   X X   X X X   X X X X X X X X X   X X         X X     X X X X   
    X X   X   X   X X X     X                       X X X   X X   X   X     X     
    X X X X   X         X   X X X X X X X X X X X X X X   X       X X   X X   X X 
            X   X   X X       X X X X X     X X X       X       X X X         X   
X       X         X   X X X X   X     X X     X X     X X           X   X       X 
X     X       X X X X X     X   X X X X   X X X     X       X X X X   X   X X   X 
  X X X X X               X     X X X   X       X X   X X   X X X X     X X       
X             X         X   X X   X X     X     X     X   X   X X X X             
    X   X X       X     X       X   X X X X X X   X X   X X X X X X X X X   X   X 
    X         X X   X       X     X   X   X       X     X X X     X       X X X X 
X     X X     X X X X X X             X X X   X               X   X     X     X X 
X   X X     X               X X X X X     X X     X X X X X X X X     X   X   X X 
X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X 
X           X     X X X X     X     X         X         X   X       X X   X X X   
X   X   X X   X X X   X         X X     X X X X     X X   X   X     X   X       X 
      X     X     X     X X     X   X X   X X   X         X X       X       X   X 
X       X           X   X   X     X X   X               X     X     X X X         
",
    );
}

#[test]
fn testAztecWriter() {
    let shift_jis: CharacterSet =
        CharacterSet::get_character_set_by_name("Shift_JIS").expect("must exist");

    testWriter("Espa\u{00F1}ol", None, 25, true, 1); // Without ECI (implicit ISO-8859-1)
    testWriter("Espa\u{00F1}ol", Some(ISO_8859_1), 25, true, 1); // Explicit ISO-8859-1
    testWriter("\u{20AC} 1 sample data.", Some(WINDOWS_1252), 25, true, 2); // ISO-8859-1 can't encode Euro; Windows-1252 can
    testWriter("\u{20AC} 1 sample data.", Some(WINDOWS_1252), 5, true, 2); // ISO-8859-1 can't encode Euro; Windows-1252 can
    testWriter("\u{20AC} 1 sample data.", Some(ISO_8859_15), 25, true, 2);
    testWriter("\u{20AC} 1 sample data.", Some(ISO_8859_15), 0, true, 2);
    testWriter(
        "\u{20AC} 1 sample data.",
        Some(CharacterSet::UTF16BE),
        0,
        true,
        3,
    );
    testWriter("Espa\u{00F1}ol", Some(UTF_8), 25, true, 2);
    testWriter("\u{20AC} 1 sample data", Some(UTF_8), 0, true, 2);
    testWriter("\u{20AC} 1 sample data. ", Some(UTF_8), 0, true, 2);
    testWriter("\u{20AC} 1 sample data .", Some(UTF_8), 0, true, 2);
    testWriter("\u{20AC} 1 sample data-.", Some(UTF_8), 0, true, 2);
    testWriter("\u{20AC} 1 sample datA.", Some(UTF_8), 0, true, 2);
    testWriter("\u{20AC} 1 sample dat1.", Some(UTF_8), 0, true, 2);
    testWriter("\u{20AC} 1 sample data.", Some(UTF_8), 0, true, 2);
    testWriter("\u{20AC} 1 sample data.", Some(UTF_8), 25, true, 2);
    testWriter("\u{20AC} 1 sample data.", Some(UTF_8), 100, true, 3);
    testWriter("\u{20AC} 1 sample data.", Some(UTF_8), 300, true, 4);
    testWriter("\u{20AC} 1 sample data.", Some(UTF_8), 500, false, 5);
    testWriter(
        "The capital of Japan is named \u{6771}\u{4EAC}.",
        Some(shift_jis),
        25,
        true,
        3,
    );
    // Test AztecWriter defaults
    let data = "In ut magna vel mauris malesuada";
    let writer = AztecWriter {};
    let matrix = writer
        .encode(data, &BarcodeFormat::AZTEC, 0, 0)
        .expect("matrix must exist");
    let aztec = aztec_encoder::encode(
        data,
        aztec_encoder::DEFAULT_EC_PERCENT,
        aztec_encoder::DEFAULT_AZTEC_LAYERS,
    )
    .expect("encode should succeed");
    let expected_matrix = aztec.getMatrix();
    assert_eq!(&matrix, expected_matrix);
}

// synthetic tests (encode-decode round-trip)

#[test]
fn testEncodeDecode1() {
    testEncodeDecode("Abc123!", true, 1);
}

#[test]
fn testEncodeDecode2() {
    testEncodeDecode("Lorem ipsum. http://test/", true, 2);
}

#[test]
fn testEncodeDecode3() {
    testEncodeDecode(
        "AAAANAAAANAAAANAAAANAAAANAAAANAAAANAAAANAAAANAAAAN",
        true,
        3,
    );
}

#[test]
fn testEncodeDecode4() {
    testEncodeDecode("http://test/~!@#*^%&)__ ;:'\"[]{}\\|-+-=`1029384", true, 4);
}

#[test]
fn testEncodeDecode5() {
    testEncodeDecode(
        r#"http://test/~!@#*^%&)__ ;:'"[]{}\|-+-=`1029384756<>/?abc
Four score and seven our forefathers brought forth"#,
        false,
        5,
    );
}

#[test]
fn testEncodeDecode10() {
    testEncodeDecode(
        "In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum sit amet. Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar nisi, id elementum sapien dolor et diam.",
        false,
        10,
    );
}

#[test]
fn testEncodeDecode23() {
    testEncodeDecode(
        "In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus quis diam\
 cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec laoreet rutrum\
 est, nec convallis mauris condimentum sit amet. Phasellus gravida, justo et congue\
 auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec lorem. Nulla\
 ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar nisi, id\
 elementum sapien dolor et diam. Donec ac nunc sodales elit placerat eleifend.\
 Sed ornare luctus ornare. Vestibulum vehicula, massa at pharetra fringilla, risus\
 justo faucibus erat, nec porttitor nibh tellus sed est. Ut justo diam, lobortis eu\
 tristique ac, p.In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus\
 quis diam cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec\
 laoreet rutrum est, nec convallis mauris condimentum sit amet. Phasellus gravida\
 justo et congue auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec\
 lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar\
 nisi, id elementum sapien dolor et diam. Donec ac nunc sodales elit placerat\
 eleifend. Sed ornare luctus ornare. Vestibulum vehicula, massa at pharetra\
 fringilla, risus justo faucibus erat, nec porttitor nibh tellus sed est. Ut justo\
 diam, lobortis eu tristique ac, p. In ut magna vel mauris malesuada dictum. Nulla\
 ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo rutrum\
 sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum sit amet.\
 Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eget hendrerit\
 felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo\
 erat pulvinar nisi, id elementum sapien dolor et diam.",
        false,
        23,
    );
}

#[test]
fn testEncodeDecode31() {
    testEncodeDecode(
        "In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus quis diam\
 cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec laoreet rutrum\
 est, nec convallis mauris condimentum sit amet. Phasellus gravida, justo et congue\
 auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec lorem. Nulla\
 ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar nisi, id\
 elementum sapien dolor et diam. Donec ac nunc sodales elit placerat eleifend.\
 Sed ornare luctus ornare. Vestibulum vehicula, massa at pharetra fringilla, risus\
 justo faucibus erat, nec porttitor nibh tellus sed est. Ut justo diam, lobortis eu\
 tristique ac, p.In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus\
 quis diam cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec\
 laoreet rutrum est, nec convallis mauris condimentum sit amet. Phasellus gravida,\
 justo et congue auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec\
 lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar\
 nisi, id elementum sapien dolor et diam. Donec ac nunc sodales elit placerat\
 eleifend. Sed ornare luctus ornare. Vestibulum vehicula, massa at pharetra\
 fringilla, risus justo faucibus erat, nec porttitor nibh tellus sed est. Ut justo\
 diam, lobortis eu tristique ac, p. In ut magna vel mauris malesuada dictum. Nulla\
 ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo rutrum\
 sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum sit amet.\
 Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eget hendrerit\
 felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo\
 erat pulvinar nisi, id elementum sapien dolor et diam. Donec ac nunc sodales elit\
 placerat eleifend. Sed ornare luctus ornare. Vestibulum vehicula, massa at\
 pharetra fringilla, risus justo faucibus erat, nec porttitor nibh tellus sed est.\
 Ut justo diam, lobortis eu tristique ac, p.In ut magna vel mauris malesuada\
 dictum. Nulla ullamcorper metus quis diam cursus facilisis. Sed mollis quam id\
 justo rutrum sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum\
 sit amet. Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat,\
 eget hendrerit felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet\
 laoreet, justo erat pulvinar nisi, id elementum sapien dolor et diam. Donec ac\
 nunc sodales elit placerat eleifend. Sed ornare luctus ornare. Vestibulum vehicula,\
 massa at pharetra fringilla, risus justo faucibus erat, nec porttitor nibh tellus\
 sed est. Ut justo diam, lobortis eu tris. In ut magna vel mauris malesuada dictum.\
 Nulla ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo rutrum\
 sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum sit amet.\
 Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eget\
 hendrerit felis turpis nec lorem.",
        false,
        31,
    );
}

#[test]
fn testGenerateModeMessage() {
    testModeMessageComplex(true, 2, 29, ".X .XXX.. ...X XX.. ..X .XX. .XX.X");
    testModeMessageComplex(true, 4, 64, "XX XXXXXX .X.. ...X ..XX .X.. XX..");
    testModeMessageComplex(
        false,
        21,
        660,
        "X.X.. .X.X..X..XX .XXX ..X.. .XXX. .X... ..XXX",
    );
    testModeMessageComplex(
        false,
        32,
        4096,
        "XXXXX XXXXXXXXXXX X.X. ..... XXX.X ..X.. X.XXX",
    );
}

#[test]
fn testStuffBitsTest() {
    testStuffBits(5, ".X.X. X.X.X .X.X.", ".X.X. X.X.X .X.X.");
    testStuffBits(5, ".X.X. ..... .X.X", ".X.X. ....X ..X.X");
    testStuffBits(
        3,
        "XX. ... ... ..X XXX .X. ..",
        "XX. ..X ..X ..X ..X .XX XX. .X. ..X",
    );
    testStuffBits(6, ".X.X.. ...... ..X.XX", ".X.X.. .....X. ..X.XX XXXX.");
    testStuffBits(
        6,
        ".X.X.. ...... ...... ..X.X.",
        ".X.X.. .....X .....X ....X. X.XXXX",
    );
    testStuffBits(
        6,
        ".X.X.. XXXXXX ...... ..X.XX",
        ".X.X.. XXXXX. X..... ...X.X XXXXX.",
    );
    testStuffBits(6,
        "...... ..XXXX X..XX. .X.... .X.X.X .....X .X.... ...X.X .....X ....XX ..X... ....X. X..XXX X.XX.X",
        ".....X ...XXX XX..XX ..X... ..X.X. X..... X.X... ....X. X..... X....X X..X.. .....X X.X..X XXX.XX .XXXXX");
}

#[test]
fn testHighLevelEncode() {
    testHighLevelEncodeString(
        "A. b.",
        // 'A'  P/S   '. ' L/L    b    D/L    '.'
        "...X. ..... ...XX XXX.. ...XX XXXX. XX.X",
    );
    testHighLevelEncodeString(
        "Lorem ipsum.",
        // 'L'  L/L   'o'   'r'   'e'   'm'   ' '   'i'   'p'   's'   'u'   'm'   D/L   '.'
        ".XX.X XXX.. X.... X..XX ..XX. .XXX. ....X .X.X. X...X X.X.. X.XX. .XXX. XXXX. XX.X",
    );
    testHighLevelEncodeString("Lo. Test 123.",
        // 'L'  L/L   'o'   P/S   '. '  U/S   'T'   'e'   's'   't'    D/L   ' '  '1'  '2'  '3'  '.'
        ".XX.X XXX.. X.... ..... ...XX XXX.. X.X.X ..XX. X.X.. X.X.X  XXXX. ...X ..XX .X.. .X.X XX.X");
    testHighLevelEncodeString(
        "Lo...x",
        // 'L'  L/L   'o'   D/L   '.'  '.'  '.'  U/L  L/L   'x'
        ".XX.X XXX.. X.... XXXX. XX.X XX.X XX.X XXX. XXX.. XX..X",
    );
    testHighLevelEncodeString(". x://abc/.",
        //P/S   '. '  L/L   'x'   P/S   ':'   P/S   '/'   P/S   '/'   'a'   'b'   'c'   P/S   '/'   D/L   '.'
        "..... ...XX XXX.. XX..X ..... X.X.X ..... X.X.. ..... X.X.. ...X. ...XX ..X.. ..... X.X.. XXXX. XX.X");
    // Uses Binary/Shift rather than Lower/Shift to save two bits.
    testHighLevelEncodeString(
        "ABCdEFG",
        //'A'   'B'   'C'   B/S    =1    'd'     'E'   'F'   'G'
        "...X. ...XX ..X.. XXXXX ....X .XX..X.. ..XX. ..XXX .X...",
    );

    testHighLevelEncodeStringCount(
        // Found on an airline boarding pass.  Several stretches of Binary shift are
        // necessary to keep the bitcount so low.
        "09  UAG    ^160MEUCIQC0sYS/HpKxnBELR1uB85R20OoqqwFGa0q2uEiYgh6utAIgLl1aBVM4EOTQtMQQYH9M2Z3Dp4qnA/fwWuQ+M8L3V8U=",
        823);
}

#[test]
fn testHighLevelEncodeBinary() {
    // binary short form single byte
    testHighLevelEncodeString(
        "N\0N",
        // 'N'  B/S    =1   '\0'      N
        ".XXXX XXXXX ....X ........ .XXXX",
    ); // Encode "N" in UPPER

    testHighLevelEncodeString(
        "N\0n",
        // 'N'  B/S    =2   '\0'       'n'
        ".XXXX XXXXX ...X. ........ .XX.XXX.",
    ); // Encode "n" in BINARY

    // binary short form consecutive bytes
    testHighLevelEncodeString(
        "N\0\u{0080} A",
        // 'N'  B/S    =2    '\0'    \u0080   ' '  'A'
        ".XXXX XXXXX ...X. ........ X....... ....X ...X.",
    );

    // binary skipping over single character
    testHighLevelEncodeString(
        "\0a\u{00FF}\u{0080} A",
        // B/S  =4    '\0'      'a'     '\3ff'   '\200'   ' '   'A'
        "XXXXX ..X.. ........ .XX....X XXXXXXXX X....... ....X ...X.",
    );

    // getting into binary mode from digit mode
    testHighLevelEncodeString(
        "1234\0",
        //D/L   '1'  '2'  '3'  '4'  U/L  B/S    =1    \0
        "XXXX. ..XX .X.. .X.X .XX. XXX. XXXXX ....X ........",
    );

    testHighLevelEncodeStringUtf8("\u{20AC} 1 sample data.", 
    "...........X..X..X...XXXXX...XXXXX...X.X.....X.X.X.XX..XXXX....X..XX...XXXX.XXX..X.X.....X..XXX.X...X.XX.X..XX.....X..X.X...X.X.X.X...X.XXXX.XX.X");

    // Create a string in which every character requires binary
    let mut sb = String::new();
    for i in 0..3000 {
        // for (int i = 0; i <= 3000; i++) {
        sb.push(char::from_u32(128 + (i % 30)).unwrap());
    }
    // Test the output generated by Binary/Switch, particularly near the
    // places where the encoding changes: 31, 62, and 2047+31=2078
    for i in [
        1, 2, 3, 10, 29, 30, 31, 32, 33, 60, 61, 62, 63, 64, 2076, 2077, 2078, 2079, 2080, 2100,
    ] {
        // for (int i : new int[] { 1, 2, 3, 10, 29, 30, 31, 32, 33,
        //                          60, 61, 62, 63, 64, 2076, 2077, 2078, 2079, 2080, 2100 }) {
        // This is the expected length of a binary string of length "i"
        let expected_length = (8 * i)
            + if i <= 31 {
                10
            } else if i <= 62 {
                20
            } else if i <= 2078 {
                21
            } else {
                31
            };
        // ( (i <= 31) ? 10 : (i <= 62) ? 20 : (i <= 2078) ? 21 : 31);
        // Verify that we are correct about the length.
        let substring_for_test: String = sb.chars().take(i).collect();
        testHighLevelEncodeStringCount(&substring_for_test, expected_length as u32);
        if i != 1 && i != 32 && i != 2079 {
            // The addition of an 'a' at the beginning or end gets merged into the binary code
            // in those cases where adding another binary character only adds 8 or 9 bits to the result.
            // So we exclude the border cases i=1,32,2079
            // A lower case letter at the beginning will be merged into binary mode
            let substring_for_sub_test: String = sb.chars().take(i - 1).collect();
            testHighLevelEncodeStringCount(
                &format!("a{}", &substring_for_sub_test),
                expected_length as u32,
            );
            // A lower case letter at the end will also be merged into binary mode
            testHighLevelEncodeStringCount(
                &format!("{}a", &substring_for_sub_test),
                expected_length as u32,
            );
        }
        // A lower case letter at both ends will enough to latch us into LOWER.
        testHighLevelEncodeStringCount(
            &format!("a{}b", &substring_for_test),
            expected_length as u32 + 15,
        );
    }

    sb.clear();

    // sb.push('A');
    for _i in 0..32 {
        // for (int i = 0; i < 32; i++) {
        sb.push('§'); // § forces binary encoding
    }
    sb.replace_range(
        sb.char_indices()
            .nth(1)
            .map(|(pos, ch)| pos..pos + ch.len_utf8())
            .unwrap(),
        "A",
    );
    // sb.setCharAt(1, 'A');
    // expect B/S(1) A B/S(30)
    testHighLevelEncodeStringCount(&sb, 5 + 20 + 31 * 8);

    sb.clear();

    // sb.push('A');
    for _i in 0..31 {
        // for (int i = 0; i < 31; i++) {
        sb.push('§');
    }
    // sb.replace_range(1..2, "A");
    sb.replace_range(
        sb.char_indices()
            .nth(1)
            .map(|(pos, ch)| pos..pos + ch.len_utf8())
            .unwrap(),
        "A",
    );
    // sb.setCharAt(1, 'A');
    // expect B/S(31)
    testHighLevelEncodeStringCount(&sb, 10 + 31 * 8);

    sb.clear();

    // sb.push('A');
    for _i in 0..34 {
        // for (int i = 0; i < 34; i++) {
        sb.push('§');
    }
    //sb.replace_range(1..2, "A");
    sb.replace_range(
        sb.char_indices()
            .nth(1)
            .map(|(pos, ch)| pos..pos + ch.len_utf8())
            .unwrap(),
        "A",
    );
    // sb.setCharAt(1, 'A');
    // expect B/S(31) B/S(3)
    testHighLevelEncodeStringCount(&sb, 20 + 34 * 8);

    sb.clear();

    for _i in 0..64 {
        // for (int i = 0; i < 64; i++) {
        sb.push('§');
    }
    //sb.replace_range(30..31, "A");
    sb.replace_range(
        sb.char_indices()
            .nth(30)
            .map(|(pos, ch)| pos..pos + ch.len_utf8())
            .unwrap(),
        "A",
    );
    // sb.setCharAt(30, 'A');
    // expect B/S(64)
    testHighLevelEncodeStringCount(&sb, 21 + 64 * 8);
}

#[test]
fn testHighLevelEncodePairs() {
    // Typical usage
    testHighLevelEncodeString(
        "ABC. DEF\r\n",
        //  A     B    C    P/S   .<sp>   D    E     F    P/S   \r\n
        "...X. ...XX ..X.. ..... ...XX ..X.X ..XX. ..XXX ..... ...X.",
    );

    // We should latch to PUNCT mode, rather than shift.  Also check all pairs
    testHighLevelEncodeString(
        "A. : , \r\n",
        // 'A'    M/L   P/L   ". "  ": "   ", " "\r\n"
        "...X. XXX.X XXXX. ...XX ..X.X  ..X.. ...X.",
    );

    // Latch to DIGIT rather than shift to PUNCT
    testHighLevelEncodeString(
        "A. 1234",
        // 'A'  D/L   '.'  ' '  '1' '2'   '3'  '4'
        "...X. XXXX. XX.X ...X ..XX .X.. .X.X .X X.",
    );
    // Don't bother leaving Binary Shift.
    testHighLevelEncodeString(
        "A\u{80}. \u{80}",
        // 'A'  B/S    =2    \200      "."     " "     \200
        "...X. XXXXX ..X.. X....... ..X.XXX. ..X..... X.......",
    );
}

#[test]
#[should_panic]
fn testUserSpecifiedLayers() {
    doTestUserSpecifiedLayers(33);
}

#[test]
#[should_panic]
fn testUserSpecifiedLayers2() {
    doTestUserSpecifiedLayers(-1);
}

fn doTestUserSpecifiedLayers(userSpecifiedLayers: i32) {
    let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let mut aztec = aztec_encoder::encode(alphabet, 25, -2).expect("should encode");
    assert_eq!(2, aztec.getLayers());
    assert!(aztec.isCompact());

    aztec = aztec_encoder::encode(alphabet, 25, 32).expect("should encode");
    assert_eq!(32, aztec.getLayers());
    assert!(!aztec.isCompact());

    aztec_encoder::encode(alphabet, 25, userSpecifiedLayers).expect("encode");
}

#[test]
#[should_panic]
fn testBorderCompact4CaseFailed() {
    // Compact(4) con hold 608 bits of information, but at most 504 can be data.  Rest must
    // be error correction
    let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    // encodes as 26 * 5 * 4 = 520 bits of data
    let alphabet4 = format!("{alphabet}{alphabet}{alphabet}{alphabet}");
    aztec_encoder::encode(&alphabet4, 0, -4).expect("encode");
}

#[test]
fn testBorderCompact4Case() {
    // Compact(4) con hold 608 bits of information, but at most 504 can be data.  Rest must
    // be error correction
    let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    // encodes as 26 * 5 * 4 = 520 bits of data
    let alphabet4 = format!("{alphabet}{alphabet}{alphabet}{alphabet}");

    // If we just try to encode it normally, it will go to a non-compact 4 layer
    let mut aztecCode = aztec_encoder::encode(&alphabet4, 0, aztec_encoder::DEFAULT_AZTEC_LAYERS)
        .expect("Should encode");
    assert!(!aztecCode.isCompact());
    assert_eq!(4, aztecCode.getLayers());

    // But shortening the string to 100 bytes (500 bits of data), compact works fine, even if we
    // include more error checking.
    aztecCode = aztec_encoder::encode(&alphabet4[..100], 10, aztec_encoder::DEFAULT_AZTEC_LAYERS)
        .expect("should encode");
    assert!(aztecCode.isCompact());
    assert_eq!(4, aztecCode.getLayers());
}

// Helper routines

fn testEncode(data: &str, compact: bool, layers: u32, expected: &str) {
    let aztec = aztec_encoder::encode(data, 33, aztec_encoder::DEFAULT_AZTEC_LAYERS)
        .expect("should encode");
    assert_eq!(
        compact,
        aztec.isCompact(),
        "Unexpected symbol format (compact)"
    );
    assert_eq!(layers, aztec.getLayers(), "Unexpected nr. of layers");
    let matrix = aztec.getMatrix();

    // let mut xored = BitMatrix::parse_strings(&stripSpace(expected), "X ", "  ").expect("should parse");
    // xored.xor(matrix).expect("should xor");
    assert_eq!(expected, matrix.to_string(), "encode({data}) failed");
}

fn testEncodeDecode(data: &str, compact: bool, layers: u32) {
    let aztec = aztec_encoder::encode(data, 25, aztec_encoder::DEFAULT_AZTEC_LAYERS)
        .expect("should encode");
    assert_eq!(
        compact,
        aztec.isCompact(),
        "Unexpected symbol format (compact)"
    );
    assert_eq!(layers, aztec.getLayers(), "Unexpected nr. of layers");
    let mut matrix = aztec.getMatrix().clone();
    let mut r = AztecDetectorRXingResult::new(
        matrix.clone(),
        NO_POINTS,
        aztec.isCompact(),
        aztec.getCodeWords(),
        aztec.getLayers(),
    );
    let mut res = decoder::decode(&r).expect("decode ok");
    assert_eq!(data, res.getText());
    // Check error correction by introducing a few minor errors
    let mut random = getPseudoRandom();
    matrix.flip_coords(
        random.gen_range(0..matrix.getWidth()),
        random.gen_range(0..=2),
    );
    matrix.flip_coords(
        random.gen_range(0..matrix.getWidth()),
        matrix.getHeight() - 2 + random.gen_range(0..2),
    );
    matrix.flip_coords(
        random.gen_range(0..=2),
        random.gen_range(0..matrix.getHeight()),
    );
    matrix.flip_coords(
        matrix.getWidth() - 2 + random.gen_range(0..2),
        random.gen_range(0..matrix.getHeight()),
    );
    r = AztecDetectorRXingResult::new(
        matrix,
        NO_POINTS,
        aztec.isCompact(),
        aztec.getCodeWords(),
        aztec.getLayers(),
    );
    res = decoder::decode(&r).expect("decode should work");
    assert_eq!(data, res.getText());
}

fn testWriter(
    data: &str,
    charset: Option<CharacterSet>,
    ecc_percent: u32,
    compact: bool,
    layers: u32,
) {
    // Perform an encode-decode round-trip because it can be lossy.
    let mut hints = HashMap::new();
    if charset.is_some() {
        hints.insert(
            EncodeHintType::CHARACTER_SET,
            EncodeHintValue::CharacterSet(charset.unwrap().get_charset_name().to_string()),
        );
    }
    // if (null != charset) {
    //   hints.put(EncodeHintType.CHARACTER_SET, charset.name());
    // }
    hints.insert(
        EncodeHintType::ERROR_CORRECTION,
        EncodeHintValue::ErrorCorrection(ecc_percent.to_string()),
    );
    let mut matrix = AztecWriter {}
        .encode_with_hints(data, &BarcodeFormat::AZTEC, 0, 0, &hints)
        .expect("encoder created");

    let cset = match charset {
        Some(cs) => cs,
        None => CharacterSet::ISO8859_1,
    };
    let aztec = aztec_encoder::encode_with_charset(
        data,
        ecc_percent,
        aztec_encoder::DEFAULT_AZTEC_LAYERS,
        cset,
    )
    .expect("encode should encode");
    assert_eq!(
        compact,
        aztec.isCompact(),
        "Unexpected symbol format (compact)"
    );
    assert_eq!(layers, aztec.getLayers(), "Unexpected nr. of layers");
    let matrix2 = aztec.getMatrix();
    assert_eq!(&matrix, matrix2);

    let mut r = AztecDetectorRXingResult::new(
        matrix.clone(),
        NO_POINTS,
        aztec.isCompact(),
        aztec.getCodeWords(),
        aztec.getLayers(),
    );

    let mut res = decoder::decode(&r).expect("should decode");
    assert_eq!(data, res.getText());

    // Check error correction by introducing up to eccPercent/2 errors
    let ec_words = aztec.getCodeWords() * ecc_percent / 100 / 2;
    let mut random = getPseudoRandom();
    for _i in 0..ec_words {
        // for (int i = 0; i < ecWords; i++) {
        // don't touch the core
        let x = if random.gen_bool(0.50) {
            random.gen_range(0..=aztec.getLayers() * 2)
        } else {
            matrix.getWidth() - 1 - random.gen_range(0..=aztec.getLayers() * 2)
        };
        let y = if random.gen_bool(0.50) {
            random.gen_range(0..=aztec.getLayers() * 2)
        } else {
            matrix.getHeight() - 1 - random.gen_range(0..=aztec.getLayers() * 2)
        };
        matrix.flip_coords(x, y);
    }
    r = AztecDetectorRXingResult::new(
        matrix,
        NO_POINTS,
        aztec.isCompact(),
        aztec.getCodeWords(),
        aztec.getLayers(),
    );
    res = decoder::decode(&r).expect("must decode");
    assert_eq!(data, res.getText());
}

fn getPseudoRandom() -> rand::rngs::ThreadRng {
    rand::thread_rng()
}

fn testModeMessageComplex(compact: bool, layers: u32, words: u32, expected: &str) {
    let indata = aztec_encoder::generateModeMessage(compact, layers, words).expect("generate mode");
    assert_eq!(
        stripSpace(expected),
        stripSpace(&indata.to_string()),
        "generateModeMessage() failed"
    );
}

fn testStuffBits(wordSize: usize, bits: &str, expected: &str) {
    let indata = toBitArray(bits);
    let stuffed = aztec_encoder::stuffBits(&indata, wordSize).unwrap();
    assert_eq!(
        stripSpace(expected),
        stripSpace(&stuffed.to_string()),
        "stuffBits() failed for input string: {bits}"
    );
}

fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) {
    let bits = HighLevelEncoder::with_charset(
        CharacterSet::UTF8
            .encode(s)
            .expect("should encode to bytes"),
        CharacterSet::UTF8,
    )
    .encode()
    .expect("high level ok");
    // let bits =  HighLevelEncoder::new(s.getBytes(StandardCharsets.ISO_8859_1)).encode();
    let receivedBits = stripSpace(&bits.to_string());
    assert_eq!(
        s,
        decoder::highLevelDecode(&toBooleanArray(&bits)).expect("must decode")
    );
    // dbg!(s, decoder::highLevelDecode(&toBooleanArray(&bits)).expect("must decode"));
    assert_eq!(
        stripSpace(expectedBits),
        receivedBits,
        "highLevelEncode() failed for input string: {s}"
    );
}

fn testHighLevelEncodeString(s: &str, expectedBits: &str) {
    let bits = HighLevelEncoder::new(
        CharacterSet::ISO8859_1
            .encode(s)
            .expect("should encode to bytes"),
    )
    .encode()
    .expect("high level ok");
    // let bits =  HighLevelEncoder::new(s.getBytes(StandardCharsets.ISO_8859_1)).encode();
    let receivedBits = stripSpace(&bits.to_string());
    assert_eq!(
        stripSpace(expectedBits),
        receivedBits,
        "highLevelEncode() failed for input string: {s}"
    );
    assert_eq!(
        s,
        decoder::highLevelDecode(&toBooleanArray(&bits)).expect("must decode")
    );
}

fn testHighLevelEncodeStringCount(s: &str, expectedReceivedBits: u32) {
    let bits = HighLevelEncoder::new(
        CharacterSet::ISO8859_1
            .encode(s)
            .expect("should encode to bytes"),
    )
    .encode()
    .expect("high level ok");
    //let bits =  HighLevelEncoder::new(s.getBytes(StandardCharsets.ISO_8859_1)).encode().unwrap();
    let receivedBitCount = stripSpace(&bits.to_string()).len();
    // dbg!(s, decoder::highLevelDecode(&toBooleanArray(&bits)).expect("should decode"));
    assert_eq!(
        s,
        decoder::highLevelDecode(&toBooleanArray(&bits)).expect("should decode")
    );
    // assert!(
    //     expectedReceivedBits as usize >= receivedBitCount,
    //     "encode size too high ({} >= {}) failed for input string: {}",
    //     expectedReceivedBits, receivedBitCount,
    //     s
    // );
    assert_eq!(
        expectedReceivedBits as usize, receivedBitCount,
        "highLevelEncode() failed for input string: {s} with byte count ({expectedReceivedBits}!={receivedBitCount})"
    );
}