1// Copyright 2018 The Bazel Authors. All rights reserved. 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 15package shard 16 17import ( 18 "testing" 19) 20 21func TestFNV(t *testing.T) { 22 tests := []struct { 23 name string 24 in []string 25 sc int 26 want []int 27 }{ 28 { 29 name: "shardCount2", 30 in: []string{"foo", "bar", "baz"}, 31 sc: 2, 32 want: []int{1, 0, 0}, 33 }, 34 { 35 name: "shardCount5", 36 in: []string{"foo", "bar", "baz"}, 37 sc: 5, 38 want: []int{0, 2, 0}, 39 }, 40 { 41 name: "shardCount9", 42 in: []string{"foo", "bar", "baz"}, 43 sc: 9, 44 want: []int{2, 7, 6}, 45 }, 46 } 47 for _, test := range tests { 48 t.Run(test.name, func(t *testing.T) { 49 for idx, in := range test.in { 50 if shard := FNV(in, test.sc); shard != test.want[idx] { 51 t.Errorf("FNV applied for: %q got: %v wanted: %v", in, shard, test.want[idx]) 52 } 53 } 54 }) 55 } 56} 57 58func TestMakeSepFunc(t *testing.T) { 59 tests := []struct { 60 name string 61 sep string 62 in []string 63 sc int 64 want []int 65 }{ 66 { 67 name: "makeSepFunc", 68 sep: "@", 69 in: []string{"foo@postfix", "bar@postfix", "baz@postfix"}, 70 sc: 9, 71 want: []int{2, 7, 6}, 72 }, 73 { 74 name: "makeSepFuncWithNoSep", 75 sep: "", 76 in: []string{"foo", "bar", "baz"}, 77 sc: 9, 78 want: []int{2, 7, 6}, 79 }, 80 { 81 name: "makeSepFuncWithWrongSep", 82 sep: "*", 83 in: []string{"foo", "bar", "baz"}, 84 sc: 9, 85 want: []int{2, 7, 6}, 86 }, 87 } 88 for _, test := range tests { 89 t.Run(test.name, func(t *testing.T) { 90 for idx, in := range test.in { 91 shardFn := MakeSepFunc(test.sep, FNV) 92 if shard := shardFn(in, test.sc); shard != test.want[idx] { 93 t.Errorf("MakeSepFunc applied for: %q got: %v wanted: %v", in, shard, test.want[idx]) 94 } 95 } 96 }) 97 } 98} 99