• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

HDT3213 / godis / 5561080764

15 Jul 2023 07:24AM UTC coverage: 73.196% (+0.05%) from 73.149%
5561080764

push

github

HDT3213
chore: use t.Errorf(...) instead of t.Error(fmt.Sprintf(...))

7660 of 10465 relevant lines covered (73.2%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

18.42
/datastruct/sortedset/border.go
1
package sortedset
2

3
import (
4
        "errors"
5
        "strconv"
6
)
7

8
/*
9
 * ScoreBorder is a struct represents `min` `max` parameter of redis command `ZRANGEBYSCORE`
10
 * can accept:
11
 *   int or float value, such as 2.718, 2, -2.718, -2 ...
12
 *   exclusive int or float value, such as (2.718, (2, (-2.718, (-2 ...
13
 *   infinity: +inf, -inf, inf(same as +inf)
14
 */
15

16
const (
17
        scoreNegativeInf int8 = -1
18
        scorePositiveInf int8 = 1
19
        lexNegativeInf   int8 = '-'
20
        lexPositiveInf   int8 = '+'
21
)
22

23
type Border interface {
24
        greater(element *Element) bool
25
        less(element *Element) bool
26
        getValue() interface{}
27
        getExclude() bool
28
        isIntersected(max Border) bool
29
}
30

31
// ScoreBorder represents range of a float value, including: <, <=, >, >=, +inf, -inf
32
type ScoreBorder struct {
33
        Inf     int8
34
        Value   float64
35
        Exclude bool
36
}
37

38
// if max.greater(score) then the score is within the upper border
39
// do not use min.greater()
40
func (border *ScoreBorder) greater(element *Element) bool {
1✔
41
        value := element.Score
1✔
42
        if border.Inf == scoreNegativeInf {
1✔
43
                return false
×
44
        } else if border.Inf == scorePositiveInf {
2✔
45
                return true
1✔
46
        }
1✔
47
        if border.Exclude {
×
48
                return border.Value > value
×
49
        }
×
50
        return border.Value >= value
×
51
}
52

53
func (border *ScoreBorder) less(element *Element) bool {
1✔
54
        value := element.Score
1✔
55
        if border.Inf == scoreNegativeInf {
2✔
56
                return true
1✔
57
        } else if border.Inf == scorePositiveInf {
2✔
58
                return false
×
59
        }
×
60
        if border.Exclude {
1✔
61
                return border.Value < value
×
62
        }
×
63
        return border.Value <= value
1✔
64
}
65

66
func (border *ScoreBorder) getValue() interface{} {
×
67
        return border.Value
×
68
}
×
69

70
func (border *ScoreBorder) getExclude() bool {
1✔
71
        return border.Exclude
1✔
72
}
1✔
73

74
var scorePositiveInfBorder = &ScoreBorder{
75
        Inf: scorePositiveInf,
76
}
77

78
var scoreNegativeInfBorder = &ScoreBorder{
79
        Inf: scoreNegativeInf,
80
}
81

82
// ParseScoreBorder creates ScoreBorder from redis arguments
83
func ParseScoreBorder(s string) (Border, error) {
×
84
        if s == "inf" || s == "+inf" {
×
85
                return scorePositiveInfBorder, nil
×
86
        }
×
87
        if s == "-inf" {
×
88
                return scoreNegativeInfBorder, nil
×
89
        }
×
90
        if s[0] == '(' {
×
91
                value, err := strconv.ParseFloat(s[1:], 64)
×
92
                if err != nil {
×
93
                        return nil, errors.New("ERR min or max is not a float")
×
94
                }
×
95
                return &ScoreBorder{
×
96
                        Inf:     0,
×
97
                        Value:   value,
×
98
                        Exclude: true,
×
99
                }, nil
×
100
        }
101
        value, err := strconv.ParseFloat(s, 64)
×
102
        if err != nil {
×
103
                return nil, errors.New("ERR min or max is not a float")
×
104
        }
×
105
        return &ScoreBorder{
×
106
                Inf:     0,
×
107
                Value:   value,
×
108
                Exclude: false,
×
109
        }, nil
×
110
}
111

112
func (border *ScoreBorder) isIntersected(max Border) bool {
1✔
113
        minValue := border.Value
1✔
114
        maxValue := max.(*ScoreBorder).Value
1✔
115
        return minValue > maxValue || (minValue == maxValue && (border.getExclude() || max.getExclude()))
1✔
116
}
1✔
117

118
// LexBorder represents range of a string value, including: <, <=, >, >=, +, -
119
type LexBorder struct {
120
        Inf     int8
121
        Value   string
122
        Exclude bool
123
}
124

125
// if max.greater(lex) then the lex is within the upper border
126
// do not use min.greater()
127
func (border *LexBorder) greater(element *Element) bool {
×
128
        value := element.Member
×
129
        if border.Inf == lexNegativeInf {
×
130
                return false
×
131
        } else if border.Inf == lexPositiveInf {
×
132
                return true
×
133
        }
×
134
        if border.Exclude {
×
135
                return border.Value > value
×
136
        }
×
137
        return border.Value >= value
×
138
}
139

140
func (border *LexBorder) less(element *Element) bool {
×
141
        value := element.Member
×
142
        if border.Inf == lexNegativeInf {
×
143
                return true
×
144
        } else if border.Inf == lexPositiveInf {
×
145
                return false
×
146
        }
×
147
        if border.Exclude {
×
148
                return border.Value < value
×
149
        }
×
150
        return border.Value <= value
×
151
}
152

153
func (border *LexBorder) getValue() interface{} {
×
154
        return border.Value
×
155
}
×
156

157
func (border *LexBorder) getExclude() bool {
×
158
        return border.Exclude
×
159
}
×
160

161
var lexPositiveInfBorder = &LexBorder{
162
        Inf: lexPositiveInf,
163
}
164

165
var lexNegativeInfBorder = &LexBorder{
166
        Inf: lexNegativeInf,
167
}
168

169
// ParseLexBorder creates LexBorder from redis arguments
170
func ParseLexBorder(s string) (Border, error) {
×
171
        if s == "+" {
×
172
                return lexPositiveInfBorder, nil
×
173
        }
×
174
        if s == "-" {
×
175
                return lexNegativeInfBorder, nil
×
176
        }
×
177
        if s[0] == '(' {
×
178
                return &LexBorder{
×
179
                        Inf:     0,
×
180
                        Value:   s[1:],
×
181
                        Exclude: true,
×
182
                }, nil
×
183
        }
×
184

185
        if s[0] == '[' {
×
186
                return &LexBorder{
×
187
                        Inf:     0,
×
188
                        Value:   s[1:],
×
189
                        Exclude: false,
×
190
                }, nil
×
191
        }
×
192

193
        return nil, errors.New("ERR min or max not valid string range item")
×
194
}
195

196
func (border *LexBorder) isIntersected(max Border) bool {
×
197
        minValue := border.Value
×
198
        maxValue := max.(*LexBorder).Value
×
199
        return border.Inf == '+' || minValue > maxValue || (minValue == maxValue && (border.getExclude() || max.getExclude()))
×
200
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc