multi-base 1.0.2

multibase in rust
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
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
#![allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]

use multi_base::{Base, Base::*, decode, decode_into, encode};

fn encode_decode_assert(input: &[u8], test_cases: Vec<(Base, &str)>) {
    for (base, output) in test_cases {
        assert_eq!(encode(base, input), output);
        assert_eq!(decode(output, true).unwrap(), (base, input.to_vec()));
    }
}

#[test]
fn test_bases_code() {
    assert_eq!(Identity.code(), '\x00');
    assert_eq!(Base2.code(), '0');
}

#[test]
fn test_bases_from_code() {
    assert_eq!(Base::from_code('\x00').unwrap(), Identity);
    assert_eq!(Base::from_code('0').unwrap(), Base2);
}

#[test]
fn test_round_trip() {
    let test_cases: &[&str] = &[
        "helloworld",
        "we all want decentralization",
        "zdj7WfBb6j58iSJuAzDcSZgy2SxFhdpJ4H87uvMpfyN6hRGyH",
    ];

    for case in test_cases {
        let encoded = encode(Base58Btc, case);
        let decoded = decode(encoded, true).unwrap();
        assert_eq!(decoded, (Base58Btc, case.as_bytes().to_vec()));
    }
}

#[test]
fn test_basic() {
    let input = b"yes mani !";
    let test_cases = vec![
        (Identity, "\x00yes mani !"),
        (
            Base2,
            "001111001011001010111001100100000011011010110000101101110011010010010000000100001",
        ),
        (Base8, "7362625631006654133464440102"),
        (Base10, "9573277761329450583662625"),
        (Base16Lower, "f796573206d616e692021"),
        (Base16Upper, "F796573206D616E692021"),
        (Base32Lower, "bpfsxgidnmfxgsibb"),
        (Base32Upper, "BPFSXGIDNMFXGSIBB"),
        (Base32HexLower, "vf5in683dc5n6i811"),
        (Base32HexUpper, "VF5IN683DC5N6I811"),
        (Base32PadLower, "cpfsxgidnmfxgsibb"),
        (Base32PadUpper, "CPFSXGIDNMFXGSIBB"),
        (Base32HexPadLower, "tf5in683dc5n6i811"),
        (Base32HexPadUpper, "TF5IN683DC5N6I811"),
        (Base32Z, "hxf1zgedpcfzg1ebb"),
        (Base36Lower, "k2lcpzo5yikidynfl"),
        (Base36Upper, "K2LCPZO5YIKIDYNFL"),
        (Base58Flickr, "Z7Pznk19XTTzBtx"),
        (Base58Btc, "z7paNL19xttacUY"),
        (Base64, "meWVzIG1hbmkgIQ"),
        (Base64Pad, "MeWVzIG1hbmkgIQ=="),
        (Base64Url, "ueWVzIG1hbmkgIQ"),
        (Base64UrlPad, "UeWVzIG1hbmkgIQ=="),
        (Base256Emoji, "🚀🏃✋🌈😅🌷🤤😻🌟😅👏"),
    ];
    encode_decode_assert(input, test_cases);
}

#[test]
fn preserves_leading_zero() {
    let input = b"\x00yes mani !";
    let test_cases = vec![
        (Identity, "\x00\x00yes mani !"),
        (
            Base2,
            "00000000001111001011001010111001100100000011011010110000101101110011010010010000000100001",
        ),
        (Base8, "7000745453462015530267151100204"),
        (Base10, "90573277761329450583662625"),
        (Base16Lower, "f00796573206d616e692021"),
        (Base16Upper, "F00796573206D616E692021"),
        (Base32Lower, "bab4wk4zanvqw42jaee"),
        (Base32Upper, "BAB4WK4ZANVQW42JAEE"),
        (Base32HexLower, "v01smasp0dlgmsq9044"),
        (Base32HexUpper, "V01SMASP0DLGMSQ9044"),
        (Base32PadLower, "cab4wk4zanvqw42jaee======"),
        (Base32PadUpper, "CAB4WK4ZANVQW42JAEE======"),
        (Base32HexPadLower, "t01smasp0dlgmsq9044======"),
        (Base32HexPadUpper, "T01SMASP0DLGMSQ9044======"),
        (Base32Z, "hybhskh3ypiosh4jyrr"),
        (Base36Lower, "k02lcpzo5yikidynfl"),
        (Base36Upper, "K02LCPZO5YIKIDYNFL"),
        (Base58Flickr, "Z17Pznk19XTTzBtx"),
        (Base58Btc, "z17paNL19xttacUY"),
        (Base64, "mAHllcyBtYW5pICE"),
        (Base64Pad, "MAHllcyBtYW5pICE="),
        (Base64Url, "uAHllcyBtYW5pICE"),
        (Base64UrlPad, "UAHllcyBtYW5pICE="),
        (Base256Emoji, "🚀🚀🏃✋🌈😅🌷🤤😻🌟😅👏"),
    ];
    encode_decode_assert(input, test_cases);
}

#[test]
fn preserves_two_leading_zeroes() {
    let input = b"\x00\x00yes mani !";
    let test_cases = vec![
        (Identity, "\x00\x00\x00yes mani !"),
        (
            Base2,
            "0000000000000000001111001011001010111001100100000011011010110000101101110011010010010000000100001",
        ),
        (Base8, "700000171312714403326055632220041"),
        (Base10, "900573277761329450583662625"),
        (Base16Lower, "f0000796573206d616e692021"),
        (Base16Upper, "F0000796573206D616E692021"),
        (Base32Lower, "baaahszltebwwc3tjeaqq"),
        (Base32Upper, "BAAAHSZLTEBWWC3TJEAQQ"),
        (Base32HexLower, "v0007ipbj41mm2rj940gg"),
        (Base32HexUpper, "V0007IPBJ41MM2RJ940GG"),
        (Base32PadLower, "caaahszltebwwc3tjeaqq===="),
        (Base32PadUpper, "CAAAHSZLTEBWWC3TJEAQQ===="),
        (Base32HexPadLower, "t0007ipbj41mm2rj940gg===="),
        (Base32HexPadUpper, "T0007IPBJ41MM2RJ940GG===="),
        (Base32Z, "hyyy813murbssn5ujryoo"),
        (Base36Lower, "k002lcpzo5yikidynfl"),
        (Base36Upper, "K002LCPZO5YIKIDYNFL"),
        (Base58Flickr, "Z117Pznk19XTTzBtx"),
        (Base58Btc, "z117paNL19xttacUY"),
        (Base64, "mAAB5ZXMgbWFuaSAh"),
        (Base64Pad, "MAAB5ZXMgbWFuaSAh"),
        (Base64Url, "uAAB5ZXMgbWFuaSAh"),
        (Base64UrlPad, "UAAB5ZXMgbWFuaSAh"),
        (Base256Emoji, "🚀🚀🚀🏃✋🌈😅🌷🤤😻🌟😅👏"),
    ];
    encode_decode_assert(input, test_cases);
}

#[test]
fn case_insensitivity() {
    let input = b"hello world";
    let test_cases = vec![
        (Base16Lower, "f68656c6c6f20776F726C64"),
        (Base16Upper, "F68656c6c6f20776F726C64"),
        (Base32Lower, "bnbswy3dpeB3W64TMMQ"),
        (Base32Upper, "Bnbswy3dpeB3W64TMMQ"),
        (Base32HexLower, "vd1imor3f41RMUSJCCG"),
        (Base32HexUpper, "Vd1imor3f41RMUSJCCG"),
        (Base32PadLower, "cnbswy3dpeB3W64TMMQ======"),
        (Base32PadUpper, "Cnbswy3dpeB3W64TMMQ======"),
        (Base32HexPadLower, "td1imor3f41RMUSJCCG======"),
        (Base32HexPadUpper, "Td1imor3f41RMUSJCCG======"),
        (Base36Lower, "kfUvrsIvVnfRbjWaJo"),
        (Base36Upper, "KfUVrSIVVnFRbJWAJo"),
    ];
    for (base, output) in test_cases {
        assert_eq!(decode(output, false).unwrap(), (base, input.to_vec()));
    }
}

// ============================================================================
// Identity Encoding Tests
// ============================================================================

/// Test that Identity encoding handles invalid UTF-8 gracefully without panicking.
#[test]
fn identity_encode_invalid_utf8_no_panic() {
    // Invalid UTF-8 sequences
    let invalid_utf8 = vec![0xFF, 0xFE, 0xFD];

    // Should not panic, uses lossy conversion
    let encoded = encode(Identity, &invalid_utf8);

    // Should start with null byte (Identity code)
    assert!(encoded.starts_with('\0'));

    // Should contain replacement characters for invalid UTF-8
    assert!(encoded.contains('\u{FFFD}'));
}

/// Test that Identity encoding properly handles valid UTF-8 including Unicode.
#[test]
fn identity_roundtrip_valid_utf8() {
    let test_cases = vec![
        "Hello, World!",
        "Hello, 世界! 🦀",
        "Rust is 🚀",
        "Émoji: 😀😃😄😁",
        "\n\t\r",
        "",
    ];

    for input in test_cases {
        let encoded = encode(Identity, input.as_bytes());
        let (base, decoded) = decode(&encoded, true).unwrap();
        assert_eq!(base, Identity);
        assert_eq!(decoded, input.as_bytes());
        assert_eq!(String::from_utf8(decoded).unwrap(), input);
    }
}

/// Test that decoding an empty string returns `EmptyInput` error.
#[test]
fn decode_empty_string_error() {
    use multi_base::Error;

    let result = decode("", true);
    assert!(result.is_err());

    match result.unwrap_err() {
        Error::EmptyInput => {} // Expected
        other => panic!("Expected EmptyInput error, got: {other:?}"),
    }
}

/// Test that unknown base codes return `UnknownBase` error.
#[test]
fn decode_unknown_base_error() {
    use multi_base::Error;

    let test_cases = vec![
        "xInvalidBase", // 'x' is not a valid base code
        "!NotABase",    // '!' is not a valid base code
        "🚫emoji",      // Emoji (except 🚀) is not a valid base code
    ];

    for input in test_cases {
        let result = decode(input, true);
        assert!(result.is_err(), "Expected error for input: {input}");

        match result.unwrap_err() {
            Error::UnknownBase { code } => {
                // Verify the error contains the invalid code
                assert_eq!(code, input.chars().next().unwrap());
            }
            other => panic!("Expected UnknownBase error for '{input}', got: {other:?}"),
        }
    }
}

/// Test that `Base::from_code` returns proper error for invalid codes.
#[test]
fn base_from_code_error() {
    use multi_base::Error;

    let invalid_codes = vec!['x', '!', '@', '#', '$', '%'];

    for code in invalid_codes {
        let result = Base::from_code(code);
        assert!(result.is_err());

        match result.unwrap_err() {
            Error::UnknownBase { code: c } => {
                assert_eq!(c, code);
            }
            other => panic!("Expected UnknownBase error, got: {other:?}"),
        }
    }
}

/// Test decoding with malformed/truncated data returns appropriate errors.
/// Note: This test verifies that truly malformed data returns errors without panicking.
#[test]
fn decode_malformed_data_errors() {
    // These should all return errors in strict mode, not panic
    let test_cases = vec![
        ("fGG", "Base16 with invalid characters (G is not hex)"),
        ("0222", "Base2 with invalid digits (2 is not binary)"),
    ];

    for (input, description) in test_cases {
        let result = decode(input, true);
        assert!(
            result.is_err(),
            "Expected error for malformed input: {input} ({description})"
        );
        // Just verify it errors - don't care about specific error type
    }

    // Test that various inputs don't panic, even if they succeed
    // (some bases are very permissive)
    let no_panic_cases = vec!["fXYZ", "mXXXX", "bXXXXX", "z123"];
    for input in no_panic_cases {
        let _ = decode(input, true); // Should not panic
        let _ = decode(input, false); // Should not panic in permissive mode either
    }
}

/// Test that error types implement expected traits.
#[test]
fn error_traits() {
    use multi_base::Error;

    let error = Error::EmptyInput;

    // Test Clone
    let cloned = error.clone();
    assert_eq!(error, cloned);

    // Test Debug
    let debug_str = format!("{error:?}");
    assert!(!debug_str.is_empty());

    // Test Display (from thiserror)
    let display_str = format!("{error}");
    assert!(!display_str.is_empty());
    assert!(display_str.contains("empty"));
}

/// Test that errors are properly converted from underlying libraries.
#[test]
fn error_conversions() {
    // Test that decode errors from various bases are properly converted
    // and don't panic

    // Invalid base2 (should only contain 0 and 1)
    let result = decode("02345", true);
    assert!(result.is_err());

    // Invalid base16 (should only contain 0-9, a-f, A-F)
    let result = decode("fXYZ", true);
    assert!(result.is_err());

    // Invalid base58 (should not contain 0, O, I, l)
    let result = decode("zOIl0", true);
    assert!(result.is_err());
}

/// Test that Identity with binary data uses lossy conversion.
#[test]
fn identity_binary_data_lossy() {
    // Test various invalid UTF-8 sequences
    let invalid_sequences = vec![
        vec![0x80],             // Invalid start byte
        vec![0xFF, 0xFF],       // Invalid bytes
        vec![0xC0, 0x80],       // Overlong encoding
        vec![b'H', 0xFF, b'i'], // Mixed valid and invalid
    ];

    for seq in invalid_sequences {
        // Should not panic
        let encoded = encode(Identity, &seq);

        // Verify it starts with Identity code
        assert_eq!(encoded.chars().next().unwrap(), Identity.code());

        // Verify the rest is valid UTF-8 (possibly with replacement characters)
        let without_code = &encoded[1..];
        assert!(std::str::from_utf8(without_code.as_bytes()).is_ok());
    }
}

// ============================================================================
// Zero-Copy Buffer Reuse Tests
// ============================================================================

/// Test `encode_into` with buffer reuse.
#[test]
fn test_encode_into_buffer_reuse() {
    use multi_base::encode_into;

    let mut buffer = String::new();

    // First encoding
    encode_into(Base58Btc, b"hello", &mut buffer);
    assert_eq!(buffer, "zCn8eVZg");

    // Reuse buffer for second encoding
    encode_into(Base64, b"world", &mut buffer);
    assert_eq!(buffer, "md29ybGQ");

    // Reuse buffer for third encoding with different data
    encode_into(Base16Lower, b"rust", &mut buffer);
    assert_eq!(buffer, "f72757374");
}

/// Test `encode_into` with all base types.
#[test]
fn test_encode_into_all_bases() {
    use multi_base::encode_into;

    let input = b"test data";
    let mut buffer = String::new();

    let bases = vec![
        Base::Identity,
        Base::Base2,
        Base::Base8,
        Base::Base10,
        Base::Base16Lower,
        Base::Base16Upper,
        Base::Base32Lower,
        Base::Base32Upper,
        Base::Base58Btc,
        Base::Base58Flickr,
        Base::Base64,
        Base::Base64Pad,
    ];

    for base in bases {
        encode_into(base, input, &mut buffer);
        assert!(buffer.starts_with(base.code()));
        assert!(!buffer.is_empty());
    }
}

/// Test `decode_into` with buffer reuse.
#[test]
fn test_decode_into_buffer_reuse() {
    use multi_base::decode_into;

    let mut buffer = Vec::new();

    // First decoding
    let base = decode_into("zCn8eVZg", true, &mut buffer).unwrap();
    assert_eq!(base, Base58Btc);
    assert_eq!(buffer, b"hello");

    // Reuse buffer for second decoding
    let base = decode_into("md29ybGQ", true, &mut buffer).unwrap();
    assert_eq!(base, Base64);
    assert_eq!(buffer, b"world");

    // Reuse buffer for third decoding
    let base = decode_into("f72757374", true, &mut buffer).unwrap();
    assert_eq!(base, Base16Lower);
    assert_eq!(buffer, b"rust");
}

/// Test that `encode_into` produces same results as encode.
#[test]
fn test_encode_into_matches_encode() {
    use multi_base::encode_into;

    let test_data = vec![
        b"".as_slice(),
        b"a",
        b"hello",
        b"the quick brown fox jumps over the lazy dog",
        &[0, 1, 2, 3, 4, 5],
        &[255, 254, 253],
    ];

    let bases = vec![
        Base::Base16Lower,
        Base::Base32Lower,
        Base::Base58Btc,
        Base::Base64,
    ];

    let mut buffer = String::new();

    for data in &test_data {
        for base in &bases {
            // Encode with regular encode
            let expected = encode(*base, data);

            // Encode with encode_into
            encode_into(*base, data, &mut buffer);

            assert_eq!(
                buffer, expected,
                "Mismatch for base {base:?} with data {data:?}"
            );
        }
    }
}

/// Test that `decode_into` produces same results as decode.
#[test]
fn test_decode_into_matches_decode() {
    let test_cases = vec![
        "zCn8eVZg",          // Base58BTC
        "md29ybGQ",          // Base64
        "f72757374",         // Base16Lower
        "BPFSXGIDNMFXGSIBB", // Base32Upper
        "001100001",         // Base2 with valid data
    ];

    let mut buffer = Vec::new();

    for input in test_cases {
        // Decode with regular decode
        let (expected_base, expected_data) = decode(input, true).unwrap();

        // Decode with decode_into
        let base = decode_into(input, true, &mut buffer).unwrap();

        assert_eq!(base, expected_base);
        assert_eq!(buffer, expected_data);
    }
}

/// Test `decode_into` error handling.
#[test]
fn test_decode_into_errors() {
    use multi_base::{Error, decode_into};

    let mut buffer = Vec::new();

    // Empty input
    let result = decode_into("", true, &mut buffer);
    assert!(result.is_err());
    assert!(matches!(result.unwrap_err(), Error::EmptyInput));

    // Unknown base
    let result = decode_into("xInvalid", true, &mut buffer);
    assert!(result.is_err());
    assert!(matches!(result.unwrap_err(), Error::UnknownBase { .. }));
}

/// Test `encode_into` with empty input.
#[test]
fn test_encode_into_empty() {
    use multi_base::encode_into;

    let mut buffer = String::new();
    encode_into(Base64, b"", &mut buffer);
    assert_eq!(buffer, "m");
}

/// Test `decode_into` with empty data (just code).
#[test]
fn test_decode_into_empty_data() {
    use multi_base::decode_into;

    let mut buffer = Vec::new();
    let base = decode_into("m", true, &mut buffer).unwrap();
    assert_eq!(base, Base64);
    assert_eq!(buffer, b"");
}

/// Test `encode_into` doesn't grow buffer unnecessarily.
#[test]
fn test_encode_into_buffer_capacity() {
    use multi_base::encode_into;

    let mut buffer = String::with_capacity(1000);
    let initial_capacity = buffer.capacity();

    // Encode small data - shouldn't need more capacity
    encode_into(Base64, b"hello", &mut buffer);

    // Capacity should remain the same or higher (never shrinks)
    assert!(buffer.capacity() >= initial_capacity);
}

/// Test `decode_into` buffer reuse in loop (performance test).
#[test]
fn test_decode_into_loop_performance() {
    use multi_base::decode_into;

    let inputs = vec!["zCn8eVZg", "md29ybGQ", "f72757374", "BPFSXGIDNMFXGSIBB"];

    let mut buffer = Vec::new();

    // This should reuse the buffer across iterations
    for input in inputs {
        let _base = decode_into(input, true, &mut buffer).unwrap();
        assert!(!buffer.is_empty());
    }
}

// ============================================================================
// EncodedString Newtype Tests
// ============================================================================

/// Test `EncodedString` basic usage.
#[test]
fn test_encoded_string_basic() {
    use multi_base::EncodedString;

    let encoded = EncodedString::new("zCn8eVZg").unwrap();
    assert_eq!(encoded.base(), Base58Btc);
    assert_eq!(encoded.as_str(), "zCn8eVZg");

    let decoded = encoded.decode().unwrap();
    assert_eq!(decoded, b"hello");
}

/// Test `EncodedString` `FromStr` implementation.
#[test]
fn test_encoded_string_from_str() {
    use multi_base::EncodedString;
    use std::str::FromStr;

    let encoded = EncodedString::from_str("md29ybGQ").unwrap();
    assert_eq!(encoded.base(), Base64);

    let parsed: EncodedString = "f48656c6c6f".parse().unwrap();
    assert_eq!(parsed.base(), Base16Lower);
}

/// Test `EncodedString` `TryFrom` implementations.
#[test]
fn test_encoded_string_try_from() {
    use multi_base::EncodedString;
    use std::convert::TryFrom;

    // From String
    let encoded = EncodedString::try_from("zCn8eVZg".to_string()).unwrap();
    assert_eq!(encoded.base(), Base58Btc);

    // From &str
    let encoded = EncodedString::try_from("md29ybGQ").unwrap();
    assert_eq!(encoded.base(), Base64);
}

/// Test `EncodedString` error handling.
#[test]
fn test_encoded_string_errors() {
    use multi_base::{EncodedString, Error};

    // Empty string
    let result = EncodedString::new("");
    assert!(result.is_err());
    assert!(matches!(result.unwrap_err(), Error::EmptyInput));

    // Invalid base code
    let result = EncodedString::new("xInvalid");
    assert!(result.is_err());
    assert!(matches!(result.unwrap_err(), Error::UnknownBase { .. }));
}

/// Test `EncodedString` validation only checks prefix.
#[test]
fn test_encoded_string_validates_prefix_only() {
    use multi_base::EncodedString;

    // This has a valid base code but invalid data
    // Construction should succeed, but decode should fail
    let encoded = EncodedString::new("fXXXXXX").unwrap();
    assert_eq!(encoded.base(), Base16Lower);

    // Decoding should fail
    let result = encoded.decode();
    assert!(result.is_err());
}

/// Test `encode_to_validated` convenience function.
#[test]
fn test_encode_to_validated() {
    use multi_base::encode_to_validated;

    let encoded = encode_to_validated(Base58Btc, b"hello");
    assert_eq!(encoded.base(), Base58Btc);
    assert_eq!(encoded.as_str(), "zCn8eVZg");

    let decoded = encoded.decode().unwrap();
    assert_eq!(decoded, b"hello");
}

/// Test `parse_encoded` convenience function.
#[test]
fn test_parse_encoded() {
    use multi_base::parse_encoded;

    let encoded = parse_encoded("zCn8eVZg").unwrap();
    assert_eq!(encoded.base(), Base58Btc);

    let result = parse_encoded("");
    assert!(result.is_err());
}

/// Test `EncodedString` with all base types.
#[test]
fn test_encoded_string_all_bases() {
    use multi_base::{EncodedString, encode_to_validated};

    let data = b"test";
    let bases = vec![Base16Lower, Base32Lower, Base58Btc, Base64];

    for base in bases {
        // Encode to validated string
        let encoded = encode_to_validated(base, data);
        assert_eq!(encoded.base(), base);

        // Decode and verify
        let decoded = encoded.decode().unwrap();
        assert_eq!(decoded, data);

        // Parse the string again
        let reparsed = EncodedString::new(encoded.as_str()).unwrap();
        assert_eq!(reparsed.base(), base);
    }
}

/// Test `EncodedString` Display trait.
#[test]
fn test_encoded_string_display() {
    use multi_base::EncodedString;

    let encoded = EncodedString::new("zCn8eVZg").unwrap();
    assert_eq!(format!("{encoded}"), "zCn8eVZg");
}

/// Test `EncodedString` `into_inner`.
#[test]
fn test_encoded_string_into_inner() {
    use multi_base::EncodedString;

    let encoded = EncodedString::new("zCn8eVZg").unwrap();
    let inner = encoded.into_inner();
    assert_eq!(inner, "zCn8eVZg");
}

/// Test `EncodedString` clone and equality.
#[test]
fn test_encoded_string_clone_eq() {
    use multi_base::EncodedString;

    let encoded1 = EncodedString::new("zCn8eVZg").unwrap();
    let encoded2 = encoded1.clone();
    let encoded3 = EncodedString::new("md29ybGQ").unwrap();

    assert_eq!(encoded1, encoded2);
    assert_ne!(encoded1, encoded3);
}

/// Test `EncodedString` `decode_with_strictness`.
#[test]
fn test_encoded_string_strictness() {
    use multi_base::EncodedString;

    // Mixed case Base16Upper (should work in permissive mode)
    let encoded = EncodedString::new("FaB").unwrap();
    assert_eq!(encoded.base(), Base16Upper);

    // Permissive mode should work
    let decoded = encoded.decode_with_strictness(false);
    assert!(decoded.is_ok());
}

// ============================================================================
// Error Handling Tests - All Bases
// ============================================================================

/// Test Base2 error handling with invalid characters.
#[test]
fn test_base2_error_handling() {
    // Base2 should only accept 0 and 1
    let invalid_cases = vec![
        "02",   // contains digit 2
        "0abc", // contains letters
        "0123", // contains digits 2 and 3
        "0   ", // contains spaces
    ];

    for input in invalid_cases {
        let result = decode(input, true);
        assert!(
            result.is_err(),
            "Base2 should reject invalid input: {input}"
        );
    }

    // Valid case - properly encoded
    let valid = encode(Base2, b"test");
    assert!(decode(&valid, true).is_ok());
}

/// Test Base8 error handling with invalid characters.
#[test]
fn test_base8_error_handling() {
    // Base8 should only accept 0-7
    let invalid_cases = vec![
        "78",   // contains digit 8
        "79",   // contains digit 9
        "7abc", // contains letters
    ];

    for input in invalid_cases {
        let result = decode(input, true);
        assert!(
            result.is_err(),
            "Base8 should reject invalid input: {input}"
        );
    }

    // Valid case - properly encoded
    let valid = encode(Base8, b"test");
    assert!(decode(&valid, true).is_ok());
}

/// Test Base10 error handling with invalid characters.
#[test]
fn test_base10_error_handling() {
    // Base10 should only accept 0-9
    let invalid_cases = vec![
        "9abc", // contains letters
        "9A",   // contains uppercase letter
        "9 12", // contains space
    ];

    for input in invalid_cases {
        let result = decode(input, true);
        assert!(
            result.is_err(),
            "Base10 should reject invalid input: {input}"
        );
    }

    // Valid case
    assert!(decode("9123456789", true).is_ok());
}

/// Test Base16 error handling with invalid characters.
#[test]
fn test_base16_error_handling() {
    // Base16 should only accept 0-9, a-f (lower) or A-F (upper)
    let invalid_cases = vec![
        "fG", // G is not a hex digit
        "fZ", // Z is not a hex digit
        "f ", // space is not valid
    ];

    for input in invalid_cases {
        let result = decode(input, true);
        assert!(
            result.is_err(),
            "Base16Lower should reject invalid input: {input}"
        );
    }

    // Valid cases
    assert!(decode("f0123456789abcdef", true).is_ok());
    assert!(decode("F0123456789ABCDEF", true).is_ok());
}

/// Test Base32 error handling with invalid characters.
#[test]
fn test_base32_error_handling() {
    // Base32 alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
    // Does not include 0, 1, 8, 9
    let invalid_cases = vec![
        "b0", // contains 0 (not in alphabet)
        "b1", // contains 1 (not in alphabet)
        "b8", // contains 8 (not in alphabet)
        "b9", // contains 9 (not in alphabet)
    ];

    for input in invalid_cases {
        let result = decode(input, true);
        assert!(
            result.is_err(),
            "Base32 should reject invalid input: {input}"
        );
    }

    // Valid case - properly encoded
    let valid = encode(Base32Lower, b"test");
    assert!(decode(&valid, true).is_ok());
}

/// Test Base36 error handling with invalid characters.
#[test]
fn test_base36_error_handling() {
    // Base36: 0-9, a-z (or A-Z)
    let invalid_cases = vec![
        "k@", // @ is not alphanumeric
        "k!", // ! is not alphanumeric
        "k ", // space is not valid
    ];

    for input in invalid_cases {
        let result = decode(input, true);
        assert!(
            result.is_err(),
            "Base36 should reject invalid input: {input}"
        );
    }

    // Valid cases
    assert!(decode("kabc123", true).is_ok());
    assert!(decode("KABC123", true).is_ok());
}

/// Test Base58 error handling with invalid characters.
#[test]
fn test_base58_error_handling() {
    // Base58 excludes 0, O, I, l to avoid confusion
    let invalid_cases = vec![
        "z0", // contains 0 (not in alphabet)
        "zO", // contains O (not in alphabet)
        "zI", // contains I (not in alphabet)
        "zl", // contains l (not in alphabet)
    ];

    for input in invalid_cases {
        let result = decode(input, true);
        assert!(
            result.is_err(),
            "Base58 should reject invalid input: {input}"
        );
    }

    // Valid case (contains characters that ARE in the alphabet)
    assert!(decode("z123ABCabc", true).is_ok());
}

/// Test Base64 error handling with invalid characters.
#[test]
fn test_base64_error_handling() {
    // Base64: A-Z, a-z, 0-9, +, /
    let invalid_cases = vec![
        "m@", // @ is not in alphabet
        "m!", // ! is not in alphabet
        "m ", // space is not valid
    ];

    for input in invalid_cases {
        let result = decode(input, true);
        assert!(
            result.is_err(),
            "Base64 should reject invalid input: {input}"
        );
    }

    // Valid case - properly encoded
    let valid = encode(Base64, b"test");
    assert!(decode(&valid, true).is_ok());
}

/// Test `Base64Url` error handling with invalid characters.
#[test]
fn test_base64url_error_handling() {
    // Base64Url: A-Z, a-z, 0-9, -, _
    let invalid_cases = vec![
        "u+", // + is Base64, not Base64Url
        "u/", // / is Base64, not Base64Url
        "u@", // @ is not in alphabet
    ];

    for input in invalid_cases {
        let result = decode(input, true);
        assert!(
            result.is_err(),
            "Base64Url should reject invalid input: {input}"
        );
    }

    // Valid case - properly encoded
    let valid = encode(Base64Url, b"test");
    assert!(decode(&valid, true).is_ok());
}

/// Test that all bases handle empty data (just the code) correctly.
#[test]
fn test_all_bases_empty_data() {
    let bases = vec![
        (Base2, "0"),
        (Base8, "7"),
        (Base10, "9"),
        (Base16Lower, "f"),
        (Base16Upper, "F"),
        (Base32Lower, "b"),
        (Base32Upper, "B"),
        (Base58Btc, "z"),
        (Base64, "m"),
        (Base64Url, "u"),
    ];

    for (expected_base, code_str) in bases {
        let result = decode(code_str, true);
        assert!(
            result.is_ok(),
            "Base {expected_base:?} should handle empty data"
        );

        let (base, data) = result.unwrap();
        assert_eq!(base, expected_base);
        assert_eq!(
            data,
            Vec::<u8>::new(),
            "Empty data should decode to empty vec"
        );
    }
}

/// Test that all bases handle single byte correctly.
#[test]
fn test_all_bases_single_byte() {
    let bases = vec![
        Base2,
        Base8,
        Base10,
        Base16Lower,
        Base16Upper,
        Base32Lower,
        Base32Upper,
        Base58Btc,
        Base64,
        Base64Url,
    ];

    let single_byte = vec![42u8]; // Arbitrary byte

    for base in bases {
        let encoded = encode(base, &single_byte);
        let (decoded_base, decoded_data) = decode(&encoded, true)
            .unwrap_or_else(|_| panic!("Failed to decode single byte for {base:?}"));

        assert_eq!(decoded_base, base);
        assert_eq!(decoded_data, single_byte);
    }
}

/// Test that strict mode is actually stricter than permissive mode.
#[test]
fn test_strict_vs_permissive_mode() {
    // Cases that should work in permissive but might fail in strict
    let test_cases = vec![
        ("f4142", Base16Lower), // Uppercase/lowercase mixing in some impls
        ("b", Base32Lower),     // Empty data
    ];

    for (input, _expected_base) in test_cases {
        // Permissive mode
        let permissive_result = decode(input, false);

        // Strict mode
        let strict_result = decode(input, true);

        // If strict succeeds, permissive must also succeed
        if let Ok((strict_base, strict_data)) = strict_result {
            assert!(
                permissive_result.is_ok(),
                "Permissive mode should succeed if strict mode succeeds for input: {input}"
            );

            let (permissive_base, permissive_data) = permissive_result.unwrap();

            assert_eq!(strict_base, permissive_base);
            assert_eq!(strict_data, permissive_data);
        }
    }
}

/// Test error messages contain useful information.
#[test]
fn test_error_messages_are_descriptive() {
    use multi_base::Error;

    // UnknownBase error should contain the invalid code
    let result = decode("xInvalid", true);
    assert!(result.is_err());
    let error_msg = format!("{}", result.unwrap_err());
    assert!(
        error_msg.contains('x'),
        "Error message should mention invalid code 'x'"
    );

    // EmptyInput error should be descriptive
    let result = decode("", true);
    assert!(result.is_err());
    match result.unwrap_err() {
        Error::EmptyInput => {
            let error_msg = format!("{}", Error::EmptyInput);
            assert!(
                error_msg.contains("empty"),
                "Error message should mention 'empty'"
            );
        }
        _ => panic!("Expected EmptyInput error"),
    }
}

/// Test that all error types are properly constructed.
#[test]
fn test_all_error_variants() {
    use multi_base::Error;

    // EmptyInput
    let err = Error::EmptyInput;
    assert!(format!("{err}").contains("empty"));

    // UnknownBase
    let err = Error::UnknownBase { code: 'x' };
    let msg = format!("{err}");
    assert!(msg.contains("unknown") || msg.contains("base"));
    assert!(msg.contains('x'));

    // InvalidBaseString
    let err = Error::InvalidBaseString;
    assert!(format!("{err}").contains("invalid"));

    // All errors should support Debug
    let err = Error::EmptyInput;
    let debug = format!("{err:?}");
    assert!(!debug.is_empty());
}

/// Test concurrent encoding/decoding doesn't cause issues.
#[test]
fn test_no_panic_under_various_inputs() {
    // Fuzz-like test with many edge case inputs
    let long_data = format!("f{}", "0".repeat(1000));
    let edge_cases = vec![
        "",         // empty
        "x",        // single invalid code
        "\0",       // null byte as code
        "f",        // just code, no data
        "f0",       // minimal valid
        "f00",      // short data
        &long_data, // long data
        "🚀",       // emoji (valid Base256Emoji code)
        "🚀test",   // Base256Emoji with data
    ];

    for input in edge_cases {
        // Should never panic
        let _ = decode(input, true);
        let _ = decode(input, false);
    }
}

/// Test that `Base32Z` uses correct alphabet.
#[test]
fn test_base32z_alphabet() {
    // Base32Z uses: ybndrfg8ejkmcpqxot1uwisza345h769
    // Does not include uppercase or some confusing chars

    let data = b"hello";
    let encoded = encode(Base32Z, data);

    // Should start with 'h' (Base32Z code)
    assert!(encoded.starts_with('h'));

    // Should roundtrip correctly
    let (base, decoded) = decode(&encoded, true).unwrap();
    assert_eq!(base, Base32Z);
    assert_eq!(decoded, data);
}

// ============================================================================
// Concurrency and Thread Safety Tests
// ============================================================================

/// Test concurrent encoding from multiple threads.
#[test]
fn test_concurrent_encoding() {
    use std::sync::Arc;
    use std::thread;

    let data = Arc::new(b"test data for concurrent encoding".to_vec());
    let bases = vec![Base16Lower, Base32Lower, Base58Btc, Base64];

    let handles: Vec<_> = bases
        .into_iter()
        .map(|base| {
            let data = Arc::clone(&data);
            thread::spawn(move || {
                let encoded = encode(base, &*data);
                let (decoded_base, decoded_data) = decode(&encoded, true).unwrap();
                assert_eq!(decoded_base, base);
                assert_eq!(decoded_data, *data);
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread panicked");
    }
}

/// Test concurrent decoding from multiple threads.
#[test]
fn test_concurrent_decoding() {
    use std::thread;

    let test_cases = vec![
        ("zCn8eVZg", Base58Btc, b"hello".to_vec()),
        ("md29ybGQ", Base64, b"world".to_vec()),
        ("f72757374", Base16Lower, b"rust".to_vec()),
        ("borsxg5a", Base32Lower, b"test".to_vec()),
    ];

    let handles: Vec<_> = test_cases
        .into_iter()
        .map(|(encoded, expected_base, expected_data)| {
            thread::spawn(move || {
                let (base, data) = decode(encoded, true).unwrap();
                assert_eq!(base, expected_base);
                assert_eq!(data, expected_data);
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread panicked");
    }
}

/// Test concurrent encoding with buffer reuse (`encode_into`).
#[test]
fn test_concurrent_encode_into() {
    use multi_base::encode_into;
    use std::thread;

    let test_data = vec![
        (b"data1".as_slice(), Base64),
        (b"data2", Base16Lower),
        (b"data3", Base58Btc),
        (b"data4", Base32Lower),
    ];

    let handles: Vec<_> = test_data
        .into_iter()
        .map(|(data, base)| {
            thread::spawn(move || {
                let mut buffer = String::new();
                encode_into(base, data, &mut buffer);

                // Verify encoding
                let (decoded_base, decoded) = decode(&buffer, true).unwrap();
                assert_eq!(decoded_base, base);
                assert_eq!(decoded, data);
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread panicked");
    }
}

/// Test concurrent decoding with buffer reuse (`decode_into`).
#[test]
fn test_concurrent_decode_into() {
    use multi_base::decode_into;
    use std::thread;

    let test_cases = vec![
        ("zCn8eVZg", Base58Btc, b"hello".to_vec()),
        ("md29ybGQ", Base64, b"world".to_vec()),
        ("f72757374", Base16Lower, b"rust".to_vec()),
        ("borsxg5a", Base32Lower, b"test".to_vec()),
    ];

    let handles: Vec<_> = test_cases
        .into_iter()
        .map(|(encoded, expected_base, expected_data)| {
            thread::spawn(move || {
                let mut buffer = Vec::new();
                let base = decode_into(encoded, true, &mut buffer).unwrap();
                assert_eq!(base, expected_base);
                assert_eq!(buffer, expected_data);
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread panicked");
    }
}

/// Test many concurrent operations to stress test thread safety.
#[test]
fn test_many_concurrent_operations() {
    use std::sync::Arc;
    use std::thread;

    let data = Arc::new(b"concurrent test data".to_vec());
    let num_threads = 20;

    let handles: Vec<_> = (0..num_threads)
        .map(|i| {
            let data = Arc::clone(&data);
            thread::spawn(move || {
                let base = match i % 4 {
                    0 => Base16Lower,
                    1 => Base32Lower,
                    2 => Base58Btc,
                    _ => Base64,
                };

                // Encode
                let encoded = encode(base, &*data);

                // Decode
                let (decoded_base, decoded_data) = decode(&encoded, true).unwrap();
                assert_eq!(decoded_base, base);
                assert_eq!(decoded_data, *data);
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread panicked");
    }
}

/// Test that `EncodedString` is Send and Sync.
#[test]
fn test_encoded_string_is_send_sync() {
    use multi_base::EncodedString;
    use std::thread;

    let encoded = EncodedString::new("zCn8eVZg").unwrap();

    // Test Send - can move between threads
    let handle = thread::spawn(move || {
        let decoded = encoded.decode().unwrap();
        assert_eq!(decoded, b"hello");
    });

    handle.join().expect("Thread panicked");
}

/// Test that `EncodedString` can be shared across threads via Arc.
#[test]
fn test_encoded_string_shared_across_threads() {
    use multi_base::EncodedString;
    use std::sync::Arc;
    use std::thread;

    let encoded = Arc::new(EncodedString::new("zCn8eVZg").unwrap());

    let handles: Vec<_> = (0..5)
        .map(|_| {
            let encoded = Arc::clone(&encoded);
            thread::spawn(move || {
                let decoded = encoded.decode().unwrap();
                assert_eq!(decoded, b"hello");
                assert_eq!(encoded.base(), Base58Btc);
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread panicked");
    }
}

/// Test concurrent `Base::from_code` calls.
#[test]
fn test_concurrent_base_from_code() {
    use std::thread;

    let codes = vec!['0', '7', '9', 'f', 'F', 'b', 'B', 'z', 'm', 'u'];

    let handles: Vec<_> = codes
        .into_iter()
        .map(|code| {
            thread::spawn(move || {
                let base = Base::from_code(code).unwrap();
                assert_eq!(base.code(), code);
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread panicked");
    }
}

/// Test concurrent operations with different data sizes.
#[test]
fn test_concurrent_various_data_sizes() {
    use std::thread;

    let test_sizes = vec![0, 1, 10, 100, 1000, 10000];

    let handles: Vec<_> = test_sizes
        .into_iter()
        .map(|size| {
            thread::spawn(move || {
                let data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();
                let encoded = encode(Base64, &data);
                let (base, decoded) = decode(&encoded, true).unwrap();
                assert_eq!(base, Base64);
                assert_eq!(decoded, data);
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread panicked");
    }
}