From 5fab2749f95c77f3ed0eb51252324637b8393343 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Fri, 24 Mar 2017 06:25:59 -0600 Subject: [PATCH] Add reverse list Former-commit-id: f45b4f86ca434997cc1d9c9263818b6064aa44f9 --- utils.go | 13 +++++++++++++ utils_test.go | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/utils.go b/utils.go index b5f257a..c339a92 100644 --- a/utils.go +++ b/utils.go @@ -247,3 +247,16 @@ func decodeFromBase32(s string) (s2 string, err error) { s2 = string(bString) return } + +func reverseSliceInt64(s []int64) []int64 { + for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { + s[i], s[j] = s[j], s[i] + } + return s +} +func reverseSliceString(s []string) []string { + for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { + s[i], s[j] = s[j], s[i] + } + return s +} diff --git a/utils_test.go b/utils_test.go index 51728ea..52479d8 100755 --- a/utils_test.go +++ b/utils_test.go @@ -4,6 +4,16 @@ import ( "testing" ) +func TestReverseList(t *testing.T) { + s := []int64{1, 10, 2, 20} + if reverseSliceInt64(s)[0] != 20 { + t.Errorf("Could not reverse: %v", s) + } + s2 := []string{"a", "b", "d", "c"} + if reverseSliceString(s2)[0] != "c" { + t.Errorf("Could not reverse: %v", s2) + } +} func TestHashing(t *testing.T) { p := HashPassword("1234") log.Debug(p)