@@ -29,15 +29,107 @@ def test_count():
29
29
assert .eq (str (c2 ), "count(11, 3)" )
30
30
assert .eq (next (c2 ), 11 )
31
31
32
+ # Negative args.
33
+ c3 = count (- 5 , - 10 )
34
+ assert .eq (str (c3 ), "count(-5, -10)" )
35
+ assert .eq (next (c3 ), - 5 )
36
+ assert .eq (str (c3 ), "count(-15, -10)" )
37
+ assert .eq (next (c3 ), - 15 )
38
+
39
+ c4 = count (5 , - 5 )
40
+ assert .eq (str (c4 ), "count(5, -5)" )
41
+ assert .eq (next (c4 ), 5 )
42
+ assert .eq (str (c4 ), "count(0, -5)" )
43
+ assert .eq (next (c4 ), 0 )
44
+ assert .eq (str (c4 ), "count(-5, -5)" )
45
+ assert .eq (next (c4 ), - 5 )
46
+
47
+ # Int start, float step.
48
+ c5 = count (0 , 0.1 )
49
+ assert .eq (str (c5 ), "count(0, 0.1)" )
50
+ assert .eq (next (c5 ), 0 )
51
+ assert .eq (str (c5 ), "count(0.1, 0.1)" )
52
+ assert .eq (next (c5 ), 0.1 )
53
+ assert .eq (str (c5 ), "count(0.2, 0.1)" )
54
+ assert .eq (next (c5 ), 0.2 )
55
+
56
+ # Float start, int step — this should be handled same as above
57
+ # but check to be exhaustive.
58
+ c6 = count (0.5 , 5 )
59
+ assert .eq (str (c6 ), "count(0.5, 5)" )
60
+ assert .eq (next (c6 ), 0.5 )
61
+ assert .eq (str (c6 ), "count(5.5, 5)" )
62
+ assert .eq (next (c6 ), 5.5 )
63
+ assert .eq (str (c6 ), "count(10.5, 5)" )
64
+ assert .eq (next (c6 ), 10.5 )
65
+
66
+ # This test may seem similar to c5 but is different because
67
+ # here step > 1. In the case that 0 < step < 1, fmt.Sprintf,
68
+ # which is used in String(), will display it as a float but
69
+ # may display it as an int if the proper flags aren't used.
70
+ c7 = count (5.0 , 0.5 )
71
+ assert .eq (str (c7 ), "count(5.0, 0.5)" )
72
+ assert .eq (next (c7 ), 5.0 )
73
+ assert .eq (str (c7 ), "count(5.5, 0.5)" )
74
+ assert .eq (next (c7 ), 5.5 )
75
+ assert .eq (str (c7 ), "count(6.0, 0.5)" )
76
+ assert .eq (next (c7 ), 6.0 )
77
+
78
+ # NaNs
79
+ c8 = count (0 , float ('nan' ))
80
+ assert .eq (str (c8 ), "count(0, %s)" % (float ('nan' )))
81
+ assert .eq (next (c8 ), 0 )
82
+ assert .eq (str (c8 ), "count(%s, %s)" % (float ('nan' ), float ('nan' )))
83
+ assert .eq (next (c8 ), float ('nan' ))
84
+ assert .eq (str (c8 ), "count(%s, %s)" % (float ('nan' ), float ('nan' )))
85
+ assert .eq (next (c8 ), float ('nan' ))
86
+
87
+ c9 = count (0 , float ("+inf" ))
88
+ assert .eq (str (c9 ), "count(0, %s)" % (float ("+inf" )))
89
+ assert .eq (next (c9 ), 0 )
90
+ assert .eq (str (c9 ), "count(%s, %s)" % (float ("+inf" ), float ("+inf" )))
91
+ assert .eq (next (c9 ), float ("+inf" ))
92
+ assert .eq (str (c9 ), "count(%s, %s)" % (float ("+inf" ), float ("+inf" )))
93
+ assert .eq (next (c9 ), float ("+inf" ))
94
+
95
+ c10 = count (0 , float ("-inf" ))
96
+ assert .eq (str (c10 ), "count(0, %s)" % (float ("-inf" )))
97
+ assert .eq (next (c10 ), 0 )
98
+ assert .eq (str (c10 ), "count(%s, %s)" % (float ("-inf" ), float ("-inf" )))
99
+ assert .eq (next (c10 ), float ("-inf" ))
100
+ assert .eq (str (c10 ), "count(%s, %s)" % (float ("-inf" ), float ("-inf" )))
101
+ assert .eq (next (c10 ), float ("-inf" ))
102
+
103
+ c11 = count (float ("nan" ), 2 )
104
+ assert .eq (str (c11 ), "count(%s, 2)" % (float ('nan' )))
105
+ assert .eq (next (c11 ), float ('nan' ))
106
+ assert .eq (str (c11 ), "count(%s, 2)" % (float ('nan' )))
107
+ assert .eq (next (c11 ), float ('nan' ))
108
+
109
+ c12 = count (float ("+inf" ), 2 )
110
+ assert .eq (str (c12 ), "count(%s, 2)" % (float ('+inf' )))
111
+ assert .eq (next (c12 ), float ('+inf' ))
112
+ assert .eq (str (c12 ), "count(%s, 2)" % (float ('+inf' )))
113
+ assert .eq (next (c12 ), float ('+inf' ))
114
+
115
+ c13 = count (float ("-inf" ), 2 )
116
+ assert .eq (str (c13 ), "count(%s, 2)" % (float ('-inf' )))
117
+ assert .eq (next (c13 ), float ('-inf' ))
118
+ assert .eq (str (c13 ), "count(%s, 2)" % (float ('-inf' )))
119
+ assert .eq (next (c13 ), float ('-inf' ))
120
+
32
121
# Fails
33
- z = ( "a" , "b" )
122
+ # Non-numeric arg fails.
34
123
assert .fails (
35
124
lambda : count ("a" , "b" ),
36
125
# fails uses match under the hood, which will use
37
126
# regexp.MatchString, so need to use raw pattern
38
127
# that MatchString would accept.
39
128
r'Got \(\"a\", \"b\"\)' ,
40
129
)
130
+
131
+ # Too many arg fails — should be handled by UnpackArgs but
132
+ # check to be exhaustive.
41
133
assert .fails (
42
134
lambda : count (1 , 2 , 3 ),
43
135
r'Got \(1, 2, 3\)'
0 commit comments