oras.land/oras-go/v2@v2.5.1-0.20240520045656-aef90e4d04c4/internal/container/set/set_test.go (about)

     1  /*
     2  Copyright The ORAS Authors.
     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  
    16  package set
    17  
    18  import "testing"
    19  
    20  func TestSet(t *testing.T) {
    21  	set := New[string]()
    22  	// test checking a non-existing key
    23  	key1 := "foo"
    24  	if got, want := set.Contains(key1), false; got != want {
    25  		t.Errorf("Set.Contains(%s) = %v, want %v", key1, got, want)
    26  	}
    27  	if got, want := len(set), 0; got != want {
    28  		t.Errorf("len(Set) = %v, want %v", got, want)
    29  	}
    30  	// test adding a new key
    31  	set.Add(key1)
    32  	if got, want := set.Contains(key1), true; got != want {
    33  		t.Errorf("Set.Contains(%s) = %v, want %v", key1, got, want)
    34  	}
    35  	if got, want := len(set), 1; got != want {
    36  		t.Errorf("len(Set) = %v, want %v", got, want)
    37  	}
    38  	// test adding an existing key
    39  	set.Add(key1)
    40  	if got, want := set.Contains(key1), true; got != want {
    41  		t.Errorf("Set.Contains(%s) = %v, want %v", key1, got, want)
    42  	}
    43  	if got, want := len(set), 1; got != want {
    44  		t.Errorf("len(Set) = %v, want %v", got, want)
    45  	}
    46  	// test adding another key
    47  	key2 := "bar"
    48  	set.Add(key2)
    49  	if got, want := set.Contains(key2), true; got != want {
    50  		t.Errorf("Set.Contains(%s) = %v, want %v", key2, got, want)
    51  	}
    52  	if got, want := len(set), 2; got != want {
    53  		t.Errorf("len(Set) = %v, want %v", got, want)
    54  	}
    55  	// test deleting a key
    56  	set.Delete(key1)
    57  	if got, want := set.Contains(key1), false; got != want {
    58  		t.Errorf("Set.Contains(%s) = %v, want %v", key1, got, want)
    59  	}
    60  	if got, want := len(set), 1; got != want {
    61  		t.Errorf("len(Set) = %v, want %v", got, want)
    62  	}
    63  }