|
| 1 | +package coll |
| 2 | + |
| 3 | +import ( |
| 4 | + "iter" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +type baseFakeWithoutInternal[V any] struct{} |
| 10 | + |
| 11 | +func (*baseFakeWithoutInternal[V]) Contains(_ Predicate[V]) bool { |
| 12 | + return false |
| 13 | +} |
| 14 | + |
| 15 | +func (*baseFakeWithoutInternal[V]) Count(_ Predicate[V]) int { |
| 16 | + return 0 |
| 17 | +} |
| 18 | + |
| 19 | +func (*baseFakeWithoutInternal[V]) Each(_ Visitor[V]) { |
| 20 | +} |
| 21 | + |
| 22 | +func (*baseFakeWithoutInternal[V]) EachUntil(_ Predicate[V]) { |
| 23 | +} |
| 24 | + |
| 25 | +func (*baseFakeWithoutInternal[V]) Find(_ Predicate[V], defaultValue V) V { |
| 26 | + return defaultValue |
| 27 | +} |
| 28 | + |
| 29 | +func (*baseFakeWithoutInternal[V]) Fold(_ Reducer[V], initial V) (result V) { |
| 30 | + return initial |
| 31 | +} |
| 32 | + |
| 33 | +func (*baseFakeWithoutInternal[V]) IsEmpty() bool { |
| 34 | + return true |
| 35 | +} |
| 36 | + |
| 37 | +func (*baseFakeWithoutInternal[V]) Len() int { |
| 38 | + return 0 |
| 39 | +} |
| 40 | + |
| 41 | +func (*baseFakeWithoutInternal[V]) Search(_ Predicate[V]) (val V, found bool) { |
| 42 | + return val, false |
| 43 | +} |
| 44 | + |
| 45 | +func (*baseFakeWithoutInternal[V]) Reduce(_ Reducer[V]) (result V, err error) { |
| 46 | + return result, nil |
| 47 | +} |
| 48 | + |
| 49 | +func (*baseFakeWithoutInternal[V]) ToSlice() []V { |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +func (*baseFakeWithoutInternal[V]) Values() iter.Seq[V] { |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func Test_Copy_forEachFlatCollection(t *testing.T) { |
| 58 | + cases := []struct { |
| 59 | + name string |
| 60 | + coll LinearMutable[int] |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "Copy() on empty Sequence", |
| 64 | + coll: NewSequence[int](), |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Copy() three-item sequence", |
| 68 | + coll: NewSequenceFrom([]int{111, 222, 333}), |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "Copy() on empty CmpSequence", |
| 72 | + coll: NewCmpSequence[int](), |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "Copy() three-item CmpSequence", |
| 76 | + coll: NewCmpSequenceFrom([]int{111, 222, 333}), |
| 77 | + }, |
| 78 | + } |
| 79 | + for _, tt := range cases { |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + got := Copy(tt.coll) |
| 82 | + if !reflect.DeepEqual(got, tt.coll) { |
| 83 | + t.Errorf("Copy() = %v, want %v", got, tt.coll) |
| 84 | + } |
| 85 | + |
| 86 | + tt.coll.Append(999) |
| 87 | + if reflect.DeepEqual(got, tt.coll) { |
| 88 | + t.Errorf("Copy() did not create a deep copy") |
| 89 | + } |
| 90 | + if got.Len() == tt.coll.Len() { |
| 91 | + t.Errorf( |
| 92 | + "Copy length %d should be different from original length %d after modification", |
| 93 | + got.Len(), |
| 94 | + tt.coll.Len(), |
| 95 | + ) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func Test_Copy_forEachMap(t *testing.T) { |
| 102 | + cases := []struct { |
| 103 | + name string |
| 104 | + coll Map[int, int] |
| 105 | + }{ |
| 106 | + { |
| 107 | + name: "Copy() on empty Map", |
| 108 | + coll: NewMap[int, int](), |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "Copy() three-item Map", |
| 112 | + coll: NewMapFrom([]Pair[int, int]{ |
| 113 | + NewPair(1, 111), |
| 114 | + NewPair(2, 222), |
| 115 | + NewPair(3, 333), |
| 116 | + }), |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "Copy() on empty CmpMap", |
| 120 | + coll: NewCmpMap[int, int](), |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "Copy() three-item CmpMap", |
| 124 | + coll: NewCmpMapFrom([]Pair[int, int]{ |
| 125 | + NewPair(1, 111), |
| 126 | + NewPair(2, 222), |
| 127 | + NewPair(3, 333), |
| 128 | + }), |
| 129 | + }, |
| 130 | + } |
| 131 | + for _, tt := range cases { |
| 132 | + t.Run(tt.name, func(t *testing.T) { |
| 133 | + got := Copy(tt.coll) |
| 134 | + if !reflect.DeepEqual(got, tt.coll) { |
| 135 | + t.Errorf("Copy() = %v, want %v", got, tt.coll) |
| 136 | + } |
| 137 | + |
| 138 | + tt.coll.Append(NewPair(4, 444)) |
| 139 | + if reflect.DeepEqual(got, tt.coll) { |
| 140 | + t.Errorf("Copy() did not create a deep copy") |
| 141 | + } |
| 142 | + if got.Len() == tt.coll.Len() { |
| 143 | + t.Errorf( |
| 144 | + "Copy length %d should be different from original length %d after modification", |
| 145 | + got.Len(), |
| 146 | + tt.coll.Len(), |
| 147 | + ) |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func Test_Copy_ofCollectionWithoutInternal(t *testing.T) { |
| 154 | + t.Run("Copy() on collection without internal", func(t *testing.T) { |
| 155 | + coll := &baseFakeWithoutInternal[int]{} |
| 156 | + defer func() { |
| 157 | + r := recover() |
| 158 | + if r == nil { |
| 159 | + t.Errorf("Copy() did not panic") |
| 160 | + } |
| 161 | + if r != "Copy() requires a collection that implements the baseInternal interface" { |
| 162 | + t.Errorf("Copy() panicked with wrong error: %v", r) |
| 163 | + } |
| 164 | + }() |
| 165 | + Copy(coll) |
| 166 | + }) |
| 167 | +} |
0 commit comments