0c157f61b9e21cc3da598c1fbf46e8a91f6e670e
[govpp.git] / vendor / github.com / onsi / gomega / matchers / be_numerically_matcher.go
1 package matchers
2
3 import (
4         "fmt"
5         "math"
6
7         "github.com/onsi/gomega/format"
8 )
9
10 type BeNumericallyMatcher struct {
11         Comparator string
12         CompareTo  []interface{}
13 }
14
15 func (matcher *BeNumericallyMatcher) FailureMessage(actual interface{}) (message string) {
16         return format.Message(actual, fmt.Sprintf("to be %s", matcher.Comparator), matcher.CompareTo[0])
17 }
18
19 func (matcher *BeNumericallyMatcher) NegatedFailureMessage(actual interface{}) (message string) {
20         return format.Message(actual, fmt.Sprintf("not to be %s", matcher.Comparator), matcher.CompareTo[0])
21 }
22
23 func (matcher *BeNumericallyMatcher) Match(actual interface{}) (success bool, err error) {
24         if len(matcher.CompareTo) == 0 || len(matcher.CompareTo) > 2 {
25                 return false, fmt.Errorf("BeNumerically requires 1 or 2 CompareTo arguments.  Got:\n%s", format.Object(matcher.CompareTo, 1))
26         }
27         if !isNumber(actual) {
28                 return false, fmt.Errorf("Expected a number.  Got:\n%s", format.Object(actual, 1))
29         }
30         if !isNumber(matcher.CompareTo[0]) {
31                 return false, fmt.Errorf("Expected a number.  Got:\n%s", format.Object(matcher.CompareTo[0], 1))
32         }
33         if len(matcher.CompareTo) == 2 && !isNumber(matcher.CompareTo[1]) {
34                 return false, fmt.Errorf("Expected a number.  Got:\n%s", format.Object(matcher.CompareTo[0], 1))
35         }
36
37         switch matcher.Comparator {
38         case "==", "~", ">", ">=", "<", "<=":
39         default:
40                 return false, fmt.Errorf("Unknown comparator: %s", matcher.Comparator)
41         }
42
43         if isFloat(actual) || isFloat(matcher.CompareTo[0]) {
44                 var secondOperand float64 = 1e-8
45                 if len(matcher.CompareTo) == 2 {
46                         secondOperand = toFloat(matcher.CompareTo[1])
47                 }
48                 success = matcher.matchFloats(toFloat(actual), toFloat(matcher.CompareTo[0]), secondOperand)
49         } else if isInteger(actual) {
50                 var secondOperand int64 = 0
51                 if len(matcher.CompareTo) == 2 {
52                         secondOperand = toInteger(matcher.CompareTo[1])
53                 }
54                 success = matcher.matchIntegers(toInteger(actual), toInteger(matcher.CompareTo[0]), secondOperand)
55         } else if isUnsignedInteger(actual) {
56                 var secondOperand uint64 = 0
57                 if len(matcher.CompareTo) == 2 {
58                         secondOperand = toUnsignedInteger(matcher.CompareTo[1])
59                 }
60                 success = matcher.matchUnsignedIntegers(toUnsignedInteger(actual), toUnsignedInteger(matcher.CompareTo[0]), secondOperand)
61         } else {
62                 return false, fmt.Errorf("Failed to compare:\n%s\n%s:\n%s", format.Object(actual, 1), matcher.Comparator, format.Object(matcher.CompareTo[0], 1))
63         }
64
65         return success, nil
66 }
67
68 func (matcher *BeNumericallyMatcher) matchIntegers(actual, compareTo, threshold int64) (success bool) {
69         switch matcher.Comparator {
70         case "==", "~":
71                 diff := actual - compareTo
72                 return -threshold <= diff && diff <= threshold
73         case ">":
74                 return (actual > compareTo)
75         case ">=":
76                 return (actual >= compareTo)
77         case "<":
78                 return (actual < compareTo)
79         case "<=":
80                 return (actual <= compareTo)
81         }
82         return false
83 }
84
85 func (matcher *BeNumericallyMatcher) matchUnsignedIntegers(actual, compareTo, threshold uint64) (success bool) {
86         switch matcher.Comparator {
87         case "==", "~":
88                 if actual < compareTo {
89                         actual, compareTo = compareTo, actual
90                 }
91                 return actual-compareTo <= threshold
92         case ">":
93                 return (actual > compareTo)
94         case ">=":
95                 return (actual >= compareTo)
96         case "<":
97                 return (actual < compareTo)
98         case "<=":
99                 return (actual <= compareTo)
100         }
101         return false
102 }
103
104 func (matcher *BeNumericallyMatcher) matchFloats(actual, compareTo, threshold float64) (success bool) {
105         switch matcher.Comparator {
106         case "~":
107                 return math.Abs(actual-compareTo) <= threshold
108         case "==":
109                 return (actual == compareTo)
110         case ">":
111                 return (actual > compareTo)
112         case ">=":
113                 return (actual >= compareTo)
114         case "<":
115                 return (actual < compareTo)
116         case "<=":
117                 return (actual <= compareTo)
118         }
119         return false
120 }