### ----------------------------------------------------
### -- CBOR::Simple
### -- Licenses: Artistic-2.0
### -- Authors: Geoffrey Broadwell
### -- File: t/01-basic.rakutest
### ----------------------------------------------------
use Test;
use lib $*PROGRAM.sibling('lib');
use CodecMatches;

use CBOR::Simple;


plan 74;


### Below adapted from https://tools.ietf.org/html/rfc8949#appendix-A

# INTEGERS

matches   0, '00';
matches   1, '01';
matches  10, '0a';
matches  23, '17';
matches  24, '1818';
matches  25, '1819';
matches 100, '1864';

matches                  1000, '1903e8';
matches               1000000, '1a000f4240';
matches         1000000000000, '1b000000e8d4a51000';
matches  18446744073709551615, '1bffffffffffffffff';
matches -18446744073709551616, '3bffffffffffffffff';
matches  18446744073709551616, 'c249010000000000000000';
matches -18446744073709551617, 'c349010000000000000000';

matches    -1, '20';
matches   -10, '29';
matches  -100, '3863';
matches -1000, '3903e7';


# FLOATS

# XXXX: Requires write support for general 16-bit nums
# matches   0.0e0, 'f90000';
# matches  -0.0e0, 'f98000';
# matches   1.0e0, 'f93c00';
# matches   1.5e0, 'f93e00';
# matches 65504e0, 'f97bff';
decodes-to   0.0e0, 'f90000';
decodes-to  -0.0e0, 'f98000';
decodes-to   1.0e0, 'f93c00';
decodes-to   1.5e0, 'f93e00';
decodes-to 65504e0, 'f97bff';

matches    1.1e0, 'fb3ff199999999999a';
matches 100000e0, 'fa47c35000';
matches 3.4028234663852886e+38, 'fa7f7fffff';
matches 1.0e+300, 'fb7e37e43c8800759c';
matches   -4.1e0, 'fbc010666666666666';

# XXXX: Requires write support for general 16-bit nums
# matches 5.960464477539063e-8, 'f90001';
# matches 0.00006103515625e0,   'f90400';
decodes-to 5.960464477539063e-8, 'f90001';
decodes-to 0.00006103515625e0,   'f90400';

# XXXX: Requires write support for general 16-bit nums
# matches -4.0e0, 'f9c400';
decodes-to -4.0e0, 'f9c400';

# ±Inf and NaN should encode to 16 bits by default
matches     Inf, 'f97c00';
matches     NaN, 'f97e00';
matches    -Inf, 'f9fc00';

decodes-to  Inf, 'fa7f800000';
decodes-to  NaN, 'fa7fc00000';
decodes-to -Inf, 'faff800000';

decodes-to  Inf, 'fb7ff0000000000000';
decodes-to  NaN, 'fb7ff8000000000000';
decodes-to -Inf, 'fbfff0000000000000';


# SIMPLE VALUES

matches False, 'f4';
matches True,  'f5';
matches Any,   'f6';
matches Mu,    'f7';


# BINARY BUFFERS

matches buf8.new,          '40';
matches buf8.new(1,2,3,4), '4401020304';


# STRINGS

matches "",       '60';
matches "a",      '6161';
matches "IETF",   '6449455446';
matches "\"\\",   '62225c';
matches "\x00fc", '62c3bc';
matches "\x6c34", '63e6b0b4';

my $utf16 = blob16.new(0xd800, 0xdd51);
my $str   = $utf16.decode('utf-16');
matches $str, '64f0908591';


# ARRAYS

matches [],                  '80';
matches [1, 2, 3],           '83010203';
matches [1, [2, 3], [4, 5]], '8301820203820405';
matches (my @ = 1..25),      '98190102030405060708090a0b0c0d0e0f101112131415161718181819';


# MAPS

matches {},                    'a0';
my %intkey{Mu} = 1, 2, 3, 4;
matches %intkey,               'a201020304';
matches {a => 1, b => [2, 3]}, 'a26161016162820203';
matches ["a", {b => "c"}],     '826161a161626163';
matches {a => "A", b => "B", c => "C", d => "D", e => "E"},
        'a56161614161626142616361436164614461656145';


# INDEFINITE LENGTH VALUES

decodes-to buf8.new(1,2,3,4,5), '5f42010243030405ff';
decodes-to "streaming",         '7f657374726561646d696e67ff';

# This test from section 3.2.3 of https://tools.ietf.org/html/rfc8949
decodes-to buf8.new(0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x99),
           '5f44aabbccdd43eeff99ff';

decodes-to [],                  '9fff';
decodes-to [1, [2, 3], [4, 5]], '9f018202039f0405ffff';
decodes-to [1, [2, 3], [4, 5]], '9f01820203820405ff';
decodes-to [1, [2, 3], [4, 5]], '83018202039f0405ff';
decodes-to [1, [2, 3], [4, 5]], '83019f0203ff820405';
decodes-to (my @ = 1..25),
           '9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff';

decodes-to {a => 1, b => [2, 3]},    'bf61610161629f0203ffff';
decodes-to ["a", {b => "c"}],        '826161bf61626163ff';
decodes-to {Fun => True, Amt => -2}, 'bf6346756ef563416d7421ff';


done-testing;


