github.com/searKing/golang/go@v1.2.117/format/multiple_prefix/decimal_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 DecimalFormatFloatCaseTest struct { 14 input float64 15 precision int 16 output string 17 } 18 19 var ( 20 decimalFormatFloatCaseTests = []DecimalFormatFloatCaseTest{ 21 { 22 input: 1234.567890, 23 precision: 1, 24 output: "1.2k", 25 }, { 26 input: 2000.567890, 27 precision: 2, 28 output: "2k", 29 }, { 30 input: 1999.567890, 31 precision: 4, 32 output: "1.9996k", 33 }, { 34 input: 1234.567890, 35 precision: 1, 36 output: "1.2k", 37 }, { 38 input: 2048.567890, 39 precision: 2, 40 output: "2.05k", 41 }, { 42 input: 1999.567890, 43 precision: 2, 44 output: "2k", 45 }, { 46 input: 123.45, 47 precision: 2, 48 output: "123.45", 49 }, { 50 input: 0.12345, 51 precision: 2, 52 output: "123.45m", 53 }, { 54 input: -0.12345, 55 precision: 2, 56 output: "-123.45m", 57 }, { 58 input: -0.00012345, 59 precision: 2, 60 output: "-123.45μ", 61 }, { 62 input: -0.0001, 63 precision: 2, 64 output: "-100μ", 65 }, 66 } 67 ) 68 69 func TestDecimalFormatFloat(t *testing.T) { 70 for n, test := range decimalFormatFloatCaseTests { 71 if got := multiple_prefix.DecimalFormatFloat(test.input, test.precision); got != test.output { 72 t.Errorf("#%d: DecimalFormatFloat(%g,%d) = %s, want %s", n, test.input, test.precision, 73 got, test.output) 74 } 75 } 76 } 77 78 type SplitDecimalCaseTest struct { 79 input string 80 outputNumber string 81 outputPrefixSymbol string 82 outputUnparsed string 83 } 84 85 var ( 86 splitDecimalCaseTests = []SplitDecimalCaseTest{ 87 { 88 input: "1234.567890HelloWorld", 89 outputNumber: "1234.567890", 90 outputPrefixSymbol: "", 91 outputUnparsed: "HelloWorld", 92 }, { 93 input: "+1234.567890\tkB", 94 outputNumber: "+1234.567890", 95 outputPrefixSymbol: "k", 96 outputUnparsed: "B", 97 }, { 98 input: "0xFFkB", 99 outputNumber: "0xFF", 100 outputPrefixSymbol: "k", 101 outputUnparsed: "B", 102 }, { 103 input: "0xFFKB", 104 outputNumber: "0xFF", 105 outputPrefixSymbol: "", 106 outputUnparsed: "KB", 107 }, 108 } 109 ) 110 111 func TestSplitDecimal(t *testing.T) { 112 for n, test := range splitDecimalCaseTests { 113 gotNumber, gotPrefix, gotUnparsed := multiple_prefix.SplitDecimal(test.input) 114 if gotPrefix == nil { 115 gotPrefix = multiple_prefix.DecimalMultiplePrefixTODO.Copy() 116 } 117 if gotNumber != test.outputNumber || gotPrefix.Symbol() != test.outputPrefixSymbol || gotUnparsed != test.outputUnparsed { 118 t.Errorf("#%d: DecimalFormatFloat(%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 }