github.com/profe-ajedrez/decimal_mula@v1.0.1/pkg/mathematics/decimal_mula_test.go (about) 1 package mathematics 2 3 import ( 4 "testing" 5 ) 6 7 func TestDecimalMulaCreation(t *testing.T) { 8 Precision = 32 9 dm1, _ := DecimalMulaFromString("1.123456789012345678901235") 10 dm2, _ := dm1.Clone() 11 dm1 = dm2 12 } 13 14 func TestDecimalMulaAdd(t *testing.T) { 15 Precision = 32 16 dm1, _ := DecimalMulaFromString("1.123456789012345678901235") 17 dm2, _ := DecimalMulaFromString("-1.1") 18 dm3, _ := DecimalMulaFromString("1.202") 19 dm4, _ := DecimalMulaFromString("1.61") 20 21 sum := dm1.Add(dm2, dm3, dm4) 22 want := "2.83545678901234567890123500000000" 23 24 if want != sum.String() { 25 t.Errorf("Expected %v, got %v at decimalMula.Add()", want, sum.String()) 26 } 27 } 28 29 func TestDecimalMulaSubstract(t *testing.T) { 30 Precision = 32 31 dm1, _ := DecimalMulaFromString("11.123456789012345678901235") 32 dm2, _ := DecimalMulaFromString("1.1") 33 dm3, _ := DecimalMulaFromString("-1.202") 34 dm4, _ := DecimalMulaFromString("1.61") 35 dm5, _ := DecimalMulaFromString("0.8989") 36 37 sub := dm1.Substract(dm2, dm3, dm4, dm5) 38 want := "8.71655678901234567890123500000000" 39 40 if want != sub.String() { 41 t.Errorf("Expected %v, got %v at decimalMula.Substract()", want, sub.String()) 42 } 43 } 44 45 func TestDecimalMulaMultiply(t *testing.T) { 46 Precision = 33 47 dm1, _ := DecimalMulaFromString("11.123456789012345678901235") 48 dm2, _ := DecimalMulaFromString("1.1") 49 dm3, _ := DecimalMulaFromString("-1.202") 50 dm4, _ := DecimalMulaFromString("1.61") 51 dm5, _ := DecimalMulaFromString("0.8989") 52 53 mul := dm1.Multiply(dm2, dm3, dm4, dm5) 54 want := "-21.285025820142995598034300386656993" 55 56 if want != mul.String() { 57 t.Errorf("Expected %v, got %v at decimalMula.Multiply()", want, mul.String()) 58 } 59 } 60 61 func TestDecimalMulaDivide(t *testing.T) { 62 Precision = 32 63 dm1, _ := DecimalMulaFromString("11.123456789012345678901235") 64 dm2, _ := DecimalMulaFromString("1.1") 65 dm3, _ := DecimalMulaFromString("-1.202") 66 dm4, _ := DecimalMulaFromString("1.61") 67 dm5, _ := DecimalMulaFromString("0.8989") 68 69 divi := dm1.Divide(dm2, dm3, dm4, dm5) 70 want := "-5.81306745796531999297392368402250" 71 72 if want != divi.String() { 73 t.Errorf("Expected %v, got %v at decimalMula.Multiply()", want, divi.String()) 74 } 75 } 76 77 func TestDecimalMulaCompare(t *testing.T) { 78 Precision = 32 79 dm1, _ := DecimalMulaFromString("11.123456789012345678901235") 80 dm2, _ := DecimalMulaFromString("11.123456789012345678901235") 81 dm3, _ := DecimalMulaFromString("1.1") 82 83 unEntero := 12 84 unFloat := 12.67 85 unString := "hola soy un texto" 86 87 if !dm1.Equals(dm2) { 88 t.Errorf("Expected %v to be equals with %v at decimalMula.Equals()", dm1.String(), dm2.String()) 89 } 90 91 if dm1.Equals(dm3) { 92 t.Errorf("Expected %v to be distinct with %v at decimalMula.Equals()", dm1.String(), dm3.String()) 93 } 94 95 if !dm1.Gt(dm3) { 96 t.Errorf("Expected %v to be greater than %v at decimalMula.Gt()", dm1.String(), dm3.String()) 97 } 98 99 if !dm3.Lt(dm1) { 100 t.Errorf("Expected %v to be lesser than %v at decimalMula.Lt()", dm3.String(), dm1.String()) 101 } 102 103 if !dm1.Gte(dm2) { 104 t.Errorf("Expected %v to be greater or equal than %v at decimalMula.Gte()", dm1.String(), dm2.String()) 105 } 106 107 if !dm1.Lte(dm2) { 108 t.Errorf("Expected %v to be lesser or equal than %v at decimalMula.Lte()", dm1.String(), dm2.String()) 109 } 110 111 if IsDecimalMula(unEntero) { 112 t.Errorf("Expected %v to be integer at isDecimalMul()", unEntero) 113 } 114 115 if IsDecimalMula(unFloat) { 116 t.Errorf("Expected %v to be float at isDecimalMul()", unFloat) 117 } 118 119 if IsDecimalMula(unString) { 120 t.Errorf("Expected %v to be string at isDecimalMul()", unString) 121 } 122 123 if !IsDecimalMula(dm1) { 124 t.Errorf("Expected %v to be decimalMula at isDecimalMul()", dm1.String()) 125 } 126 } 127 128 func BenchmarkCreateDecimalMula(b *testing.B) { 129 b.ResetTimer() 130 var d decimalMula 131 for n := 0; n < 100000; n++ { 132 d, _ = DecimalMulaFromString("1.123456789012345678901235") 133 } 134 d.Add(d, d) 135 } 136 137 func BenchmarkAddDecimalMula(b *testing.B) { 138 b.ResetTimer() 139 var d3 decimalMula 140 d1, _ := DecimalMulaFromString("1.123456789012345678901235") 141 d2, _ := DecimalMulaFromString("3457891.4123456789012345678901235") 142 for n := 0; n < 100000; n++ { 143 d3 = *d2.Add(d1) 144 } 145 d3.Add(d3, d3) 146 }