github.com/searKing/golang/go@v1.2.117/format/multiple_prefix/binary_format_test.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package multiple_prefix_test 6 7 import ( 8 "testing" 9 10 "github.com/searKing/golang/go/format/multiple_prefix" 11 ) 12 13 type BinaryFormatFloatCaseTest struct { 14 input float64 15 precision int 16 output string 17 } 18 19 var ( 20 binaryFormatFloatCaseTests = []BinaryFormatFloatCaseTest{ 21 { 22 input: 1234.567890, 23 precision: 1, 24 output: "1.2Ki", 25 }, { 26 input: 2000.567890, 27 precision: 2, 28 output: "1.95Ki", 29 }, { 30 input: 1999.567890, 31 precision: 4, 32 output: "1.9527Ki", 33 }, { 34 input: 1234.567890, 35 precision: 1, 36 output: "1.2Ki", 37 }, { 38 input: 2048.567890, 39 precision: 2, 40 output: "2Ki", 41 }, { 42 input: 1999.567890, 43 precision: 2, 44 output: "1.95Ki", 45 }, { 46 input: 123.45, 47 precision: 2, 48 output: "123.45", 49 }, { 50 input: 0.12345, 51 precision: 2, 52 output: "0.12", 53 }, { 54 input: -0.12345, 55 precision: 2, 56 output: "-0.12", 57 }, { 58 input: -0.00012345, 59 precision: 5, 60 output: "-0.00012", 61 }, { 62 input: -0.0001, 63 precision: 2, 64 output: "-0", 65 }, 66 } 67 ) 68 69 func TestBinaryFormatFloat(t *testing.T) { 70 for n, test := range binaryFormatFloatCaseTests { 71 if got := multiple_prefix.BinaryFormatFloat(test.input, test.precision); got != test.output { 72 t.Errorf("#%d: BinaryFormatFloat(%g,%d) = %s, want %s", n, test.input, test.precision, 73 got, test.output) 74 } 75 } 76 } 77 78 type SplitBinaryCaseTest struct { 79 input string 80 outputNumber string 81 outputPrefixSymbol string 82 outputUnparsed string 83 } 84 85 var ( 86 splitBinaryCaseTests = []SplitBinaryCaseTest{ 87 { 88 input: "1234.567890HelloWorld", 89 outputNumber: "1234.567890", 90 outputPrefixSymbol: "", 91 outputUnparsed: "HelloWorld", 92 }, { 93 input: "+1234.567890KiB", 94 outputNumber: "+1234.567890", 95 outputPrefixSymbol: "Ki", 96 outputUnparsed: "B", 97 }, { 98 input: "0xFFKiB", 99 outputNumber: "0xFF", 100 outputPrefixSymbol: "Ki", 101 outputUnparsed: "B", 102 }, { 103 input: "0xFFkiB", 104 outputNumber: "0xFF", 105 outputPrefixSymbol: "", 106 outputUnparsed: "kiB", 107 }, 108 } 109 ) 110 111 func TestSplitBinary(t *testing.T) { 112 for n, test := range splitBinaryCaseTests { 113 gotNumber, gotPrefix, gotUnparsed := multiple_prefix.SplitBinary(test.input) 114 if gotPrefix == nil { 115 gotPrefix = multiple_prefix.BinaryMultiplePrefixTODO.Copy() 116 } 117 if gotNumber != test.outputNumber || gotPrefix.Symbol() != test.outputPrefixSymbol || gotUnparsed != test.outputUnparsed { 118 t.Errorf("#%d: BinaryFormatFloat(%s) = (%s, %s, %s), want (%s, %s, %s)", n, test.input, 119 gotNumber, gotPrefix.Symbol(), gotUnparsed, 120 test.outputNumber, test.outputPrefixSymbol, test.outputUnparsed) 121 } 122 } 123 }