/* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://www.apache.org/licenses/LICENSE-2.0 or in the "license" file accompanying this file. This file 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. */ package cbor import ( "bytes" "strings" "testing" ) func TestLexDecimal(t *testing.T) { cases := []struct { in, out string encoded []byte }{ {in: "-1000000000001", out: "-1000000000001"}, {in: "-999999999998", out: "-999999999998"}, {in: "-123.0", out: "-1230E-1"}, {in: "-123", out: "-123"}, {in: "-90.01000", out: "-9001000E-5"}, {in: "-90.0100", out: "-900100E-4"}, {in: "-90.010", out: "-90010E-3"}, {in: "-90.01", out: "-9001E-2"}, {in: "-11.000", out: "-11000E-3"}, {in: "-11.00", out: "-1100E-2"}, {in: "-11.0", out: "-110E-1"}, {in: "-11", out: "-11"}, {in: "-10.100", out: "-10100E-3"}, {in: "-10.10", out: "-1010E-2"}, {in: "-10.1", out: "-101E-1"}, {in: "-10.010", out: "-10010E-3"}, {in: "-10.01", out: "-1001E-2"}, {in: "-10.001", out: "-10001E-3"}, {in: "-3.14", out: "-314E-2"}, {in: "-2", out: "-2"}, {in: "-1", out: "-1"}, {in: "-0.1", out: "-1E-1"}, {in: "-0.01", out: "-1E-2"}, {in: "-0.001", out: "-1E-3"}, {in: "-0.0001", out: "-1E-4"}, {in: "-0.00001", out: "-1E-5"}, {in: "-22234353.464363E-43534545", out: "-22234353464363E-43534551", encoded: fromHex("0x7e829848c8c569c775705f7fe0")}, {in: "0", out: "0"}, {in: "0.00001", out: "1E-5"}, {in: "0.0001", out: "1E-4"}, {in: "0.001", out: "1E-3"}, {in: "0.01", out: "1E-2"}, {in: "0.1", out: "1E-1"}, {in: "1", out: "1"}, {in: "1.519043396057566434978347919110056828178617793420044845009095883102266197154983036170458159785045013168790194150005772171485123776663026225265083906941746679973910405487712226922360572160109286090827076840438099560139704109021077055039800992783574226209489067204747368646064279454016947109162015587773374645982363407385318610580528401964567546584779984692390257012118847492632253017295152938061049334216698639782484923671333852898253917484438297806515060144054619575586755908264584998428966635833900328448897013161311192041184225731248515188720354249594803044541017997943733673275617711293386746906471936186661605920860928968943328237982074083977888182412651483608535562031694297058289536988244754588375618162121230365495345691015255028957668241560658402002255378192112746401695967255968778246414510329530913582678929506142265708375720504712549814629448365993242613061373583046901281817825417384505249197569842907151879339231499054003148135601842677695751809087592596946507113528743471045104423997476282338624005970522081700127468869151380640131536800078172950172445606480673750541462894055397499420148923999103971546156968314898287319971866608744671525743690473528323942745338438886549298899754829284582718694198133239949986632293934118351261598803084928864575827542573948274062057885778247343717195523808009809686102995991858298705780338006833410164829798137972194669364972243958207138283628313248174819322874644154437406662833820122947930072048521034645768459084715771457149574679024406949597278639059408919395313400229796162612035343061223639450398459653735214907021029784343593875497348473060433594883367192096557118929922580718994140625", out: "1519043396057566434978347919110056828178617793420044845009095883102266197154983036170458159785045013168790194150005772171485123776663026225265083906941746679973910405487712226922360572160109286090827076840438099560139704109021077055039800992783574226209489067204747368646064279454016947109162015587773374645982363407385318610580528401964567546584779984692390257012118847492632253017295152938061049334216698639782484923671333852898253917484438297806515060144054619575586755908264584998428966635833900328448897013161311192041184225731248515188720354249594803044541017997943733673275617711293386746906471936186661605920860928968943328237982074083977888182412651483608535562031694297058289536988244754588375618162121230365495345691015255028957668241560658402002255378192112746401695967255968778246414510329530913582678929506142265708375720504712549814629448365993242613061373583046901281817825417384505249197569842907151879339231499054003148135601842677695751809087592596946507113528743471045104423997476282338624005970522081700127468869151380640131536800078172950172445606480673750541462894055397499420148923999103971546156968314898287319971866608744671525743690473528323942745338438886549298899754829284582718694198133239949986632293934118351261598803084928864575827542573948274062057885778247343717195523808009809686102995991858298705780338006833410164829798137972194669364972243958207138283628313248174819322874644154437406662833820122947930072048521034645768459084715771457149574679024406949597278639059408919395313400229796162612035343061223639450398459653735214907021029784343593875497348473060433594883367192096557118929922580718994140625E-1641"}, {in: "2", out: "2"}, {in: "3.14", out: "314E-2", encoded: fromHex("0xc1518020")}, {in: "3.141", out: "3141E-3", encoded: fromHex("0xc151870000")}, {in: "10.001", out: "10001E-3"}, {in: "10.01", out: "1001E-2"}, {in: "10.010", out: "10010E-3"}, {in: "10.1", out: "101E-1"}, {in: "10.10", out: "1010E-2"}, {in: "10.100", out: "10100E-3"}, {in: "11", out: "11"}, {in: "11.0", out: "110E-1"}, {in: "11.00", out: "1100E-2"}, {in: "11.000", out: "11000E-3"}, {in: "90.01", out: "9001E-2"}, {in: "90.010", out: "90010E-3"}, {in: "90.0100", out: "900100E-4"}, {in: "90.01000", out: "9001000E-5"}, {in: "123", out: "123"}, {in: "123.0", out: "1230E-1"}, {in: "3.14E11", out: "314E9", encoded: fromHex("0xcc518020")}, {in: "999999999999", out: "999999999999"}, {in: "1000000000000", out: "1000000000000"}, {in: "111122223333444445555.66667777", out: "11112222333344444555566667777E-8", encoded: fromHex("0xd51ec863ad59721c98dea6ac70e004")}, {in: "111122223333444445555.66667778", out: "11112222333344444555566667778E-8", encoded: fromHex("0xd51ec863ad59721c98dea6ac718004")}, } var last []byte for _, c := range cases { var buf bytes.Buffer var dec Decimal dec.SetString(c.in) wl, err := EncodeLexDecimal(&dec, &buf) if err != nil { t.Errorf("unexpected encoding error " + err.Error()) continue } encoded := buf.Bytes() if c.encoded != nil { if wl != len(c.encoded) { t.Errorf("expected length %d, actual length %d", len(c.encoded), wl) } if bytes.Compare(c.encoded, encoded) != 0 { t.Errorf("expected=%v, actual=%v for %v\n", c.encoded, encoded, c.in) } } if last != nil && bytes.Compare(last, encoded) >= 0 { t.Errorf("expected %v to be greater than or equal to last", c.in) } last = encoded actual, err := DecodeLexDecimal(&buf) if err != nil { t.Errorf("unexpected decoding error " + err.Error()) continue } if strings.Compare(c.out, actual.String()) != 0 { t.Errorf("expected=%s, actual=%v", c.out, actual) } } } func BenchmarkLexDecimalEncode(b *testing.B) { dec := new(Decimal) dec.SetString("123456789.123") var buf bytes.Buffer b.ResetTimer() for i := 0; i < b.N; i++ { if _, err := EncodeLexDecimal(dec, &buf); err != nil { b.Errorf("unexpected error %v", err) } buf.Reset() } } func BenchmarkLexDecimalDecode(b *testing.B) { dec := new(Decimal) dec.SetString("123456789.123") var buf bytes.Buffer if _, err := EncodeLexDecimal(dec, &buf); err != nil { b.Errorf("unexpected error %v", err) } br := bytes.NewReader(buf.Bytes()) b.ResetTimer() for i := 0; i < b.N; i++ { if _, err := DecodeLexDecimal(br); err != nil { b.Errorf("unexpected error %v", err) } br.Seek(0, 0) } }