github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/hash/hash_test.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // This file incorporates work covered by the following copyright and 16 // permission notice: 17 // 18 // Copyright 2016 Attic Labs, Inc. All rights reserved. 19 // Licensed under the Apache License, version 2.0: 20 // http://www.apache.org/licenses/LICENSE-2.0 21 22 package hash 23 24 import ( 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestParseError(t *testing.T) { 31 assert := assert.New(t) 32 33 assertParseError := func(s string) { 34 assert.Panics(func() { 35 Parse(s) 36 }) 37 } 38 39 assertParseError("foo") 40 41 // too few digits 42 assertParseError("0000000000000000000000000000000") 43 44 // too many digits 45 assertParseError("000000000000000000000000000000000") 46 47 // 'w' not valid base32 48 assertParseError("00000000000000000000000000000000w") 49 50 // no prefix 51 assertParseError("sha1-00000000000000000000000000000000") 52 assertParseError("sha2-00000000000000000000000000000000") 53 54 r := Parse("00000000000000000000000000000000") 55 assert.NotNil(r) 56 } 57 58 func TestMaybeParse(t *testing.T) { 59 assert := assert.New(t) 60 61 parse := func(s string, success bool) { 62 r, ok := MaybeParse(s) 63 assert.Equal(success, ok, "Expected success=%t for %s", success, s) 64 if ok { 65 assert.Equal(s, r.String()) 66 } else { 67 assert.Equal(emptyHash, r) 68 } 69 } 70 71 parse("00000000000000000000000000000000", true) 72 parse("00000000000000000000000000000001", true) 73 parse("", false) 74 parse("adsfasdf", false) 75 parse("sha2-00000000000000000000000000000000", false) 76 parse("0000000000000000000000000000000w", false) 77 } 78 79 func TestEquals(t *testing.T) { 80 assert := assert.New(t) 81 82 r0 := Parse("00000000000000000000000000000000") 83 r01 := Parse("00000000000000000000000000000000") 84 r1 := Parse("00000000000000000000000000000001") 85 86 assert.Equal(r0, r01) 87 assert.Equal(r01, r0) 88 assert.NotEqual(r0, r1) 89 assert.NotEqual(r1, r0) 90 } 91 92 func TestString(t *testing.T) { 93 s := "0123456789abcdefghijklmnopqrstuv" 94 r := Parse(s) 95 assert.Equal(t, s, r.String()) 96 } 97 98 func TestOf(t *testing.T) { 99 r := Of([]byte("abc")) 100 assert.Equal(t, "rmnjb8cjc5tblj21ed4qs821649eduie", r.String()) 101 } 102 103 func TestIsEmpty(t *testing.T) { 104 r1 := Hash{} 105 assert.True(t, r1.IsEmpty()) 106 107 r2 := Parse("00000000000000000000000000000000") 108 assert.True(t, r2.IsEmpty()) 109 110 r3 := Parse("rmnjb8cjc5tblj21ed4qs821649eduie") 111 assert.False(t, r3.IsEmpty()) 112 } 113 114 func TestLess(t *testing.T) { 115 assert := assert.New(t) 116 117 r1 := Parse("00000000000000000000000000000001") 118 r2 := Parse("00000000000000000000000000000002") 119 120 assert.False(r1.Less(r1)) 121 assert.True(r1.Less(r2)) 122 assert.False(r2.Less(r1)) 123 assert.False(r2.Less(r2)) 124 125 r0 := Hash{} 126 assert.False(r0.Less(r0)) 127 assert.True(r0.Less(r2)) 128 assert.False(r2.Less(r0)) 129 } 130 131 func TestGreater(t *testing.T) { 132 assert := assert.New(t) 133 134 r1 := Parse("00000000000000000000000000000001") 135 r2 := Parse("00000000000000000000000000000002") 136 137 assert.False(r1.Greater(r1)) 138 assert.False(r1.Greater(r2)) 139 assert.True(r2.Greater(r1)) 140 assert.False(r2.Greater(r2)) 141 142 r0 := Hash{} 143 assert.False(r0.Greater(r0)) 144 assert.False(r0.Greater(r2)) 145 assert.True(r2.Greater(r0)) 146 }