Skip to content

Commit 8c06d11

Browse files
authored
Add missing Compare funcs (go-chrono#25)
* Refactor test names. * Rename variable. * Rename test variable. * Implement Duration.Compare. * Rename variable. * Remove unused function. * Fix typos. * Rename test variables. * Implement Instant.Compare. * Revert "Remove unused function." This reverts commit bd31c7a. * Add tests for Instant.Compare.
1 parent 776119e commit 8c06d11

7 files changed

+90
-19
lines changed

duration.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,21 @@ func DurationOf(v Extent) Duration {
2323
}
2424
}
2525

26-
// Equal reports whether d and d2 represent the same duration of time.
27-
func (d Duration) Equal(d2 Duration) bool {
28-
return d2.secs == d.secs && d2.nsec == d.nsec
26+
// Compare compares d with d2. If d is less than d2, it returns -1;
27+
// if d is greater than d2, it returns 1; if they're equal, it returns 0.
28+
func (d Duration) Compare(d2 Duration) int {
29+
switch {
30+
case d.secs < d2.secs:
31+
return -1
32+
case d.secs > d2.secs:
33+
return 1
34+
case d.nsec < d2.nsec:
35+
return -1
36+
case d.nsec > d2.nsec:
37+
return 1
38+
default:
39+
return 0
40+
}
2941
}
3042

3143
// Add returns the duration d+d2.

duration_test.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,27 @@ func TestDuration_units(t *testing.T) {
175175
}
176176
}
177177

178+
func TestDuration_Compare(t *testing.T) {
179+
for _, tt := range []struct {
180+
name string
181+
d chrono.Duration
182+
d2 chrono.Duration
183+
expected int
184+
}{
185+
{"seconds less", chrono.DurationOf(1 * chrono.Hour), chrono.DurationOf(2 * chrono.Hour), -1},
186+
{"seconds more", chrono.DurationOf(2 * chrono.Hour), chrono.DurationOf(1 * chrono.Hour), 1},
187+
{"nanos less", chrono.DurationOf(1 * chrono.Nanosecond), chrono.DurationOf(2 * chrono.Nanosecond), -1},
188+
{"nanos more", chrono.DurationOf(2 * chrono.Nanosecond), chrono.DurationOf(1 * chrono.Nanosecond), 1},
189+
{"equal", chrono.DurationOf(chrono.Minute), chrono.DurationOf(chrono.Minute), 0},
190+
} {
191+
t.Run(tt.name, func(t *testing.T) {
192+
if v := tt.d.Compare(tt.d2); v != tt.expected {
193+
t.Errorf("d.Compare(d2) = %d, want %d", v, tt.expected)
194+
}
195+
})
196+
}
197+
}
198+
178199
func TestDuration_Add(t *testing.T) {
179200
for _, tt := range []struct {
180201
name string
@@ -226,7 +247,7 @@ func TestDuration_Add(t *testing.T) {
226247
}
227248

228249
d := tt.d1.Add(tt.d2)
229-
if !d.Equal(tt.expected) {
250+
if d.Compare(tt.expected) != 0 {
230251
t.Fatalf("d1.Add(d2) = %v, want %v", d, tt.expected)
231252
}
232253
})
@@ -237,7 +258,7 @@ func TestDuration_Add(t *testing.T) {
237258
}
238259

239260
d := tt.d2.Add(tt.d1)
240-
if !d.Equal(tt.expected) {
261+
if d.Compare(tt.expected) != 0 {
241262
t.Fatalf("d2.Add(d1) = %v, want %v", d, tt.expected)
242263
}
243264
})
@@ -536,7 +557,7 @@ func TestDuration_Parse(t *testing.T) {
536557
var d chrono.Duration
537558
if err := d.Parse(tt.input); err != nil {
538559
t.Fatalf("failed to parse duation: %v", err)
539-
} else if !d.Equal(tt.expected) {
560+
} else if d.Compare(tt.expected) != 0 {
540561
t.Fatalf("parsed duration = %v, want %v", d, tt.expected)
541562
}
542563
})

instant.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ func Now() Instant {
1515
}
1616
}
1717

18+
// Compare compares i with i2. If i is before i2, it returns -1;
19+
// if i is after i2, it returns 1; if they're the same, it returns 0.
20+
func (i Instant) Compare(i2 Instant) int {
21+
switch {
22+
case i.v == nil:
23+
panic("i is not initialized")
24+
case i2.v == nil:
25+
panic("i2 is not initialized")
26+
case *i.v < *i2.v:
27+
return -1
28+
case *i.v > *i2.v:
29+
return 1
30+
default:
31+
return 0
32+
}
33+
}
34+
1835
// Elapsed is shorthand for i.Until(chrono.Now()).
1936
func (i Instant) Elapsed() Duration {
2037
return i.Until(Now())
@@ -25,15 +42,15 @@ func (i Instant) String() string {
2542
}
2643

2744
// Until returns the Duration that represents the elapsed time from i to v.
28-
func (i Instant) Until(v Instant) Duration {
45+
func (i Instant) Until(i2 Instant) Duration {
2946
switch {
3047
case i.v == nil:
3148
panic("i is not initialized")
32-
case v.v == nil:
33-
panic("v is not initialized")
49+
case i2.v == nil:
50+
panic("i2 is not initialized")
3451
}
3552

36-
iv, vv := *i.v, *v.v
53+
iv, vv := *i.v, *i2.v
3754
if vv < iv {
3855
panic("v is smaller than i")
3956
}
@@ -45,6 +62,7 @@ func (i Instant) Until(v Instant) Duration {
4562
}
4663
}
4764

65+
// instant is used by the chronotest package.
4866
func instant(t int64) Instant {
4967
return Instant{
5068
v: &t,

instant_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package chrono_test
33
import (
44
"testing"
55

6+
"github.com/go-chrono/chrono"
67
chronotest "github.com/go-chrono/chrono/test"
78
)
89

@@ -13,6 +14,25 @@ func TestInstant_String(t *testing.T) {
1314
}
1415
}
1516

17+
func TestInstant_Compare(t *testing.T) {
18+
for _, tt := range []struct {
19+
name string
20+
i chrono.Instant
21+
i2 chrono.Instant
22+
expected int
23+
}{
24+
{"earlier", chronotest.InstantOf(11e14), chronotest.InstantOf(12e14), -1},
25+
{"later", chronotest.InstantOf(12e14), chronotest.InstantOf(11e14), 1},
26+
{"equal", chronotest.InstantOf(11e14), chronotest.InstantOf(11e14), 0},
27+
} {
28+
t.Run(tt.name, func(t *testing.T) {
29+
if v := tt.i.Compare(tt.i2); v != tt.expected {
30+
t.Errorf("i.Compare(i2) = %d, want %d", v, tt.expected)
31+
}
32+
})
33+
}
34+
}
35+
1636
func TestInstant_Until(t *testing.T) {
1737
i := chronotest.InstantOf(11e14)
1838
d := i.Until(chronotest.InstantOf(12e14))

local_time.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ func (t LocalTime) Add(v Extent) LocalTime {
7373
return LocalTime{v: out}
7474
}
7575

76-
// Compare compares t with u. If t is before u, it returns -1;
77-
// if t is after u, it returns 1; if they're the same, it returns 0.
78-
func (t LocalTime) Compare(u LocalTime) int {
76+
// Compare compares t with t2. If t is before t2, it returns -1;
77+
// if t is after t2, it returns 1; if they're the same, it returns 0.
78+
func (t LocalTime) Compare(t2 LocalTime) int {
7979
switch {
80-
case t.v < u.v:
80+
case t.v < t2.v:
8181
return -1
82-
case t.v > u.v:
82+
case t.v > t2.v:
8383
return 1
8484
default:
8585
return 0

local_time_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ func TestLocalTime_Compare(t *testing.T) {
7979
for _, tt := range []struct {
8080
name string
8181
t chrono.LocalTime
82-
u chrono.LocalTime
82+
t2 chrono.LocalTime
8383
expected int
8484
}{
8585
{"earlier", chrono.LocalTimeOf(11, 0, 0, 0), chrono.LocalTimeOf(12, 0, 0, 0), -1},
8686
{"later", chrono.LocalTimeOf(13, 30, 0, 0), chrono.LocalTimeOf(13, 29, 55, 0), 1},
8787
{"equal", chrono.LocalTimeOf(15, 0, 0, 1000), chrono.LocalTimeOf(15, 0, 0, 1000), 0},
8888
} {
8989
t.Run(tt.name, func(t *testing.T) {
90-
if v := tt.t.Compare(tt.u); v != tt.expected {
91-
t.Errorf("t.Compare(u) = %d, want %d", v, tt.expected)
90+
if v := tt.t.Compare(tt.t2); v != tt.expected {
91+
t.Errorf("t.Compare(t2) = %d, want %d", v, tt.expected)
9292
}
9393
})
9494
}

period_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func TestParseDuration(t *testing.T) {
204204
t.Fatalf("failed to parse period & duration: %v", err)
205205
} else if !p.Equal(tt.period) {
206206
t.Fatalf("parsed period = %v, want %v", p, tt.period)
207-
} else if !d.Equal(tt.duration) {
207+
} else if d.Compare(tt.duration) != 0 {
208208
t.Fatalf("parsed duration = %v, want %v", d, tt.duration)
209209
}
210210
})

0 commit comments

Comments
 (0)