dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

hashset_test.go (2721B)


      1 package hashset
      2 
      3 import (
      4 	"testing"
      5 
      6 	"github.com/stretchr/testify/assert"
      7 )
      8 
      9 func TestInsert(t *testing.T) {
     10 	h1 := New[string]()
     11 	r1 := h1.Insert("key1")
     12 	assert.True(t, r1)
     13 	r2 := h1.Insert("key1")
     14 	assert.False(t, r2)
     15 }
     16 
     17 func TestUnion(t *testing.T) {
     18 	h1 := New[string]()
     19 	h1.Insert("key1")
     20 	h2 := New[string]()
     21 	h2.Insert("key2")
     22 	h3 := h1.Union(h2)
     23 	assert.True(t, h3.Contains("key1"))
     24 	assert.True(t, h3.Contains("key2"))
     25 }
     26 
     27 func TestDifference(t *testing.T) {
     28 	h1 := New[string]()
     29 	h1.Insert("key1")
     30 	h1.Insert("key2")
     31 	h2 := New[string]()
     32 	h2.Insert("key2")
     33 	h3 := h1.Difference(h2)
     34 	assert.True(t, h3.Contains("key1"))
     35 	assert.False(t, h3.Contains("key2"))
     36 
     37 	h1 = New[string]()
     38 	h1.Insert("key1")
     39 	h1.Insert("key2")
     40 	h2 = New[string]()
     41 	h2.Insert("key1")
     42 	h3 = h1.Difference(h2)
     43 	assert.False(t, h3.Contains("key1"))
     44 	assert.True(t, h3.Contains("key2"))
     45 
     46 	h1 = New[string]()
     47 	h1.Insert("key1")
     48 	h1.Insert("key2")
     49 	h2 = New[string]()
     50 	h2.Insert("key1")
     51 	h2.Insert("key3")
     52 	h3 = h1.Difference(h2)
     53 	assert.False(t, h3.Contains("key1"))
     54 	assert.True(t, h3.Contains("key2"))
     55 	assert.False(t, h3.Contains("key3"))
     56 }
     57 
     58 func TestSymmetricDifference(t *testing.T) {
     59 	h1 := New[string]()
     60 	h1.Insert("key1")
     61 	h1.Insert("key2")
     62 	h2 := New[string]()
     63 	h2.Insert("key1")
     64 	h2.Insert("key3")
     65 	h3 := h1.SymmetricDifference(h2)
     66 	assert.False(t, h3.Contains("key1"))
     67 	assert.True(t, h3.Contains("key2"))
     68 	assert.True(t, h3.Contains("key3"))
     69 }
     70 
     71 func TestIntersection(t *testing.T) {
     72 	h1 := New[string]()
     73 	h1.Insert("key1")
     74 	h1.Insert("key2")
     75 	h2 := New[string]()
     76 	h2.Insert("key1")
     77 	h2.Insert("key3")
     78 	h3 := h1.Intersection(h2)
     79 	assert.True(t, h3.Contains("key1"))
     80 	assert.False(t, h3.Contains("key2"))
     81 	assert.False(t, h3.Contains("key3"))
     82 }
     83 
     84 func TestIsSubset(t *testing.T) {
     85 	h1 := New[string]()
     86 	h1.Insert("key1")
     87 	h1.Insert("key2")
     88 	h2 := New[string]()
     89 	h2.Insert("key1")
     90 	h2.Insert("key3")
     91 	assert.False(t, h1.IsSubset(h2))
     92 
     93 	h1 = New[string]()
     94 	h1.Insert("key1")
     95 	h1.Insert("key2")
     96 	h2 = New[string]()
     97 	h2.Insert("key1")
     98 	h2.Insert("key2")
     99 	h2.Insert("key3")
    100 	assert.True(t, h1.IsSubset(h2))
    101 
    102 	h1 = New[string]()
    103 	h1.Insert("key1")
    104 	h1.Insert("key2")
    105 	h2 = New[string]()
    106 	h2.Insert("key1")
    107 	assert.False(t, h1.IsSubset(h2))
    108 }
    109 
    110 func TestIsSuperset(t *testing.T) {
    111 	h1 := New[string]()
    112 	h1.Insert("key1")
    113 	h1.Insert("key2")
    114 	h2 := New[string]()
    115 	h2.Insert("key1")
    116 	h2.Insert("key3")
    117 	assert.False(t, h1.IsSuperset(h2))
    118 
    119 	h1 = New[string]()
    120 	h1.Insert("key1")
    121 	h1.Insert("key2")
    122 	h2 = New[string]()
    123 	h2.Insert("key1")
    124 	h2.Insert("key2")
    125 	h2.Insert("key3")
    126 	assert.False(t, h1.IsSuperset(h2))
    127 
    128 	h1 = New[string]()
    129 	h1.Insert("key1")
    130 	h1.Insert("key2")
    131 	h2 = New[string]()
    132 	h2.Insert("key1")
    133 	assert.True(t, h1.IsSuperset(h2))
    134 }