github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/x/x_test.go (about) 1 /* 2 * Copyright 2017-2018 Dgraph Labs, Inc. and Contributors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package x 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestRemoveDuplicates(t *testing.T) { 26 set := RemoveDuplicates([]string{"a", "a", "a", "b", "b", "c", "c"}) 27 require.EqualValues(t, []string{"a", "b", "c"}, set) 28 } 29 30 func TestRemoveDuplicatesWithoutDuplicates(t *testing.T) { 31 set := RemoveDuplicates([]string{"a", "b", "c", "d"}) 32 require.EqualValues(t, []string{"a", "b", "c", "d"}, set) 33 } 34 35 func TestDivideAndRule(t *testing.T) { 36 test := func(num, expectedGo, expectedWidth int) { 37 numGo, width := DivideAndRule(num) 38 require.Equal(t, expectedGo, numGo) 39 require.Equal(t, expectedWidth, width) 40 } 41 42 test(68, 1, 68) 43 test(255, 1, 255) 44 test(256, 1, 256) 45 test(510, 1, 510) 46 47 test(511, 2, 256) 48 test(512, 2, 256) 49 test(513, 2, 257) 50 51 test(768, 2, 384) 52 53 test(1755, 4, 439) 54 } 55 56 func TestValidateAddress(t *testing.T) { 57 t.Run("IPv4", func(t *testing.T) { 58 testData := []struct { 59 name string 60 address string 61 isValid bool 62 }{ 63 {"Valid without port", "190.0.0.1", false}, 64 {"Valid with port", "192.5.32.1:333", true}, 65 {"Invalid without port", "12.0.0", false}, 66 // the following test returns true because 12.0.0 is considered as valid 67 // hostname 68 {"Invalid with port", "12.0.0:3333", true}, 69 } 70 for _, subtest := range testData { 71 st := subtest 72 t.Run(st.name, func(t *testing.T) { 73 require.Equal(t, st.isValid, ValidateAddress(st.address)) 74 }) 75 } 76 77 }) 78 t.Run("IPv6", func(t *testing.T) { 79 testData := []struct { 80 name string 81 address string 82 isValid bool 83 }{ 84 {"Valid without port", "[2001:db8::1]", false}, 85 {"Valid with port", "[2001:db8::1]:8888", true}, 86 {"Invalid without port", "[2001:db8]", false}, 87 {"Invalid with port", "[2001:db8]:2222", false}, 88 } 89 for _, subtest := range testData { 90 st := subtest 91 t.Run(st.name, func(t *testing.T) { 92 require.Equal(t, st.isValid, ValidateAddress(st.address)) 93 }) 94 } 95 }) 96 }