github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/pkg/set/string_test.go (about) 1 // Copyright 2015 The rkt Authors 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 // For how the uidshift and uidcount are generated please check: 16 // http://cgit.freedesktop.org/systemd/systemd/commit/?id=03cfe0d51499e86b1573d1 17 18 package set 19 20 import ( 21 "strings" 22 "testing" 23 ) 24 25 func TestString(t *testing.T) { 26 s := NewString("a", "b", "c") 27 if len(s) != 3 { 28 t.Errorf("Expected len=3: %d", len(s)) 29 } 30 if !s.Has("a") || !s.Has("b") || !s.Has("c") { 31 t.Errorf("Unexpected contents: %#v", s) 32 } 33 34 if !s.HasAll("a", "b", "c") { 35 t.Errorf("Unexpected contents: %#v", s) 36 } 37 38 if s.HasAll("a", "b", "c", "d") { 39 t.Errorf("Unexpected contents: %#v", s) 40 } 41 42 s.Delete("a", "b") 43 if len(s) != 1 { 44 t.Errorf("Expected len=1: %d", len(s)) 45 } 46 if !s.Has("c") { 47 t.Errorf("Unexpected contents: %#v", s) 48 } 49 50 s.Delete("a") 51 if len(s) != 1 { 52 t.Errorf("Expected len=1: %d", len(s)) 53 } 54 if !s.Has("c") { 55 t.Errorf("Unexpected contents: %#v", s) 56 } 57 58 s.Delete("") 59 if len(s) != 1 { 60 t.Errorf("Expected len=1: %d", len(s)) 61 } 62 if !s.Has("c") { 63 t.Errorf("Unexpected contents: %#v", s) 64 } 65 66 s.Delete("c") 67 if len(s) != 0 { 68 t.Errorf("Expected len=1: %d", len(s)) 69 } 70 if s.Has("a") || s.Has("b") || s.Has("c") { 71 t.Errorf("Unexpected contents: %#v", s) 72 } 73 } 74 75 func TestContitionalHas(t *testing.T) { 76 tests := []struct { 77 keys []string 78 item string 79 conditionFunc func(string, string) bool 80 expectResult bool 81 }{ 82 { 83 []string{"foo", "bar", "hello"}, 84 "bar", 85 func(a, b string) bool { return a == b }, 86 true, 87 }, 88 { 89 []string{"foo", "bar", "hello"}, 90 "baz", 91 func(a, b string) bool { return a == b }, 92 false, 93 }, 94 { 95 []string{"foo", "bar", "hello"}, 96 "he", 97 strings.HasPrefix, 98 true, 99 }, 100 } 101 102 for i, tt := range tests { 103 s := NewString(tt.keys...) 104 actualResult := s.ConditionalHas(tt.conditionFunc, tt.item) 105 if tt.expectResult != actualResult { 106 t.Errorf("test case %d: expected: %v, saw: %v", i, tt.expectResult, actualResult) 107 } 108 } 109 }