From d036ecbbb9eae9a0546efefe73dda9180375bfb3 Mon Sep 17 00:00:00 2001 From: RichardHightower Date: Tue, 20 Dec 2016 10:32:18 -0800 Subject: [PATCH 1/4] added time comparison --- utils/comparator.go | 17 +++++++++++++++++ utils/comparator_test.go | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/utils/comparator.go b/utils/comparator.go index 98d6e3d..ef5246a 100644 --- a/utils/comparator.go +++ b/utils/comparator.go @@ -4,6 +4,8 @@ package utils +import "time" + // Comparator will make type assertion (see IntComparator for example), // which will panic if a or b are not of the asserted type. // @@ -51,6 +53,21 @@ func IntComparator(a, b interface{}) int { } } +// IntComparator provides a basic comparison on int +func TimeComparator(a, b interface{}) int { + aAsserted := a.(time.Time) + bAsserted := b.(time.Time) + + switch { + case aAsserted.After(bAsserted) : + return 1 + case aAsserted.Before(bAsserted): + return -1 + default: + return 0 + } +} + // Int8Comparator provides a basic comparison on int8 func Int8Comparator(a, b interface{}) int { aAsserted := a.(int8) diff --git a/utils/comparator_test.go b/utils/comparator_test.go index 12849f8..00d028f 100644 --- a/utils/comparator_test.go +++ b/utils/comparator_test.go @@ -6,6 +6,7 @@ package utils import ( "testing" + "time" ) func TestIntComparator(t *testing.T) { @@ -30,6 +31,27 @@ func TestIntComparator(t *testing.T) { } } + +func TestTimeComparator(t *testing.T) { + + now := time.Now() + + // i1,i2,expected + tests := [][]interface{}{ + {now, now, 0}, + {now.Add(24 * 7 * 2 * time.Hour), now, 1}, + {now, now.Add(24 * 7 * 2 * time.Hour), -1}, + } + + for _, test := range tests { + actual := TimeComparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + func TestStringComparator(t *testing.T) { // s1,s2,expected From 1d7bfb173f7ef278b5c541d3bad82973610650cb Mon Sep 17 00:00:00 2001 From: RichardHightower Date: Tue, 20 Dec 2016 10:35:13 -0800 Subject: [PATCH 2/4] Comparator for time. --- utils/comparator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/comparator.go b/utils/comparator.go index ef5246a..b5f0f79 100644 --- a/utils/comparator.go +++ b/utils/comparator.go @@ -53,7 +53,7 @@ func IntComparator(a, b interface{}) int { } } -// IntComparator provides a basic comparison on int +// TimeComparator provides a basic comparison on time.Time func TimeComparator(a, b interface{}) int { aAsserted := a.(time.Time) bAsserted := b.(time.Time) From 9407a8206e69f6b94c5b903bb788fb69d55eb6c8 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Fri, 3 Mar 2017 23:00:46 +0100 Subject: [PATCH 3/4] - update readme about contributions (should go into development) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a833b13..46d8afb 100644 --- a/README.md +++ b/README.md @@ -1253,7 +1253,7 @@ This takes a while, so test within sub-packages: Biggest contribution towards this library is to use it and give us feedback for further improvements and additions. -For direct contributions, _pull request_ into master or ask to become a contributor. +For direct contributions, _pull request_ into development branch or ask to become a contributor. Coding style: From 3389248bfca5c19e184ae3d5f6404729c6739720 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Fri, 3 Mar 2017 23:24:20 +0100 Subject: [PATCH 4/4] - time comparator fmt and documentation update --- utils/comparator.go | 30 +++++++++++++++--------------- utils/comparator_test.go | 39 +++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/utils/comparator.go b/utils/comparator.go index b5f0f79..6a9afbf 100644 --- a/utils/comparator.go +++ b/utils/comparator.go @@ -53,21 +53,6 @@ func IntComparator(a, b interface{}) int { } } -// TimeComparator provides a basic comparison on time.Time -func TimeComparator(a, b interface{}) int { - aAsserted := a.(time.Time) - bAsserted := b.(time.Time) - - switch { - case aAsserted.After(bAsserted) : - return 1 - case aAsserted.Before(bAsserted): - return -1 - default: - return 0 - } -} - // Int8Comparator provides a basic comparison on int8 func Int8Comparator(a, b interface{}) int { aAsserted := a.(int8) @@ -249,3 +234,18 @@ func RuneComparator(a, b interface{}) int { return 0 } } + +// TimeComparator provides a basic comparison on time.Time +func TimeComparator(a, b interface{}) int { + aAsserted := a.(time.Time) + bAsserted := b.(time.Time) + + switch { + case aAsserted.After(bAsserted): + return 1 + case aAsserted.Before(bAsserted): + return -1 + default: + return 0 + } +} diff --git a/utils/comparator_test.go b/utils/comparator_test.go index 00d028f..40efbd3 100644 --- a/utils/comparator_test.go +++ b/utils/comparator_test.go @@ -31,20 +31,22 @@ func TestIntComparator(t *testing.T) { } } +func TestStringComparator(t *testing.T) { -func TestTimeComparator(t *testing.T) { - - now := time.Now() - - // i1,i2,expected + // s1,s2,expected tests := [][]interface{}{ - {now, now, 0}, - {now.Add(24 * 7 * 2 * time.Hour), now, 1}, - {now, now.Add(24 * 7 * 2 * time.Hour), -1}, + {"a", "a", 0}, + {"a", "b", -1}, + {"b", "a", 1}, + {"aa", "aab", -1}, + {"", "", 0}, + {"a", "", 1}, + {"", "a", -1}, + {"", "aaaaaaa", -1}, } for _, test := range tests { - actual := TimeComparator(test[0], test[1]) + actual := StringComparator(test[0], test[1]) expected := test[2] if actual != expected { t.Errorf("Got %v expected %v", actual, expected) @@ -52,22 +54,19 @@ func TestTimeComparator(t *testing.T) { } } -func TestStringComparator(t *testing.T) { +func TestTimeComparator(t *testing.T) { - // s1,s2,expected + now := time.Now() + + // i1,i2,expected tests := [][]interface{}{ - {"a", "a", 0}, - {"a", "b", -1}, - {"b", "a", 1}, - {"aa", "aab", -1}, - {"", "", 0}, - {"a", "", 1}, - {"", "a", -1}, - {"", "aaaaaaa", -1}, + {now, now, 0}, + {now.Add(24 * 7 * 2 * time.Hour), now, 1}, + {now, now.Add(24 * 7 * 2 * time.Hour), -1}, } for _, test := range tests { - actual := StringComparator(test[0], test[1]) + actual := TimeComparator(test[0], test[1]) expected := test[2] if actual != expected { t.Errorf("Got %v expected %v", actual, expected)