|
| 1 | +/* |
| 2 | + * MinIO Go Library for Amazon S3 Compatible Cloud Storage |
| 3 | + * Copyright 2015-2026 MinIO, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package set |
| 19 | + |
| 20 | +import ( |
| 21 | + "reflect" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +// IntSet.MarshalJSON() is called with series of cases for valid and erroneous inputs and the result is validated. |
| 26 | +func TestIntSetMarshalJSON(t *testing.T) { |
| 27 | + testCases := []struct { |
| 28 | + set IntSet |
| 29 | + expectedResult string |
| 30 | + }{ |
| 31 | + // Test set with values. |
| 32 | + {CreateIntSet(1, 2, 3), `[1,2,3]`}, |
| 33 | + // Test empty set. |
| 34 | + {NewIntSet(), "[]"}, |
| 35 | + } |
| 36 | + |
| 37 | + for _, testCase := range testCases { |
| 38 | + if result, _ := testCase.set.MarshalJSON(); string(result) != testCase.expectedResult { |
| 39 | + t.Fatalf("expected: %s, got: %s", testCase.expectedResult, string(result)) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// IntSet.UnmarshalJSON() is called with series of cases for valid and erroneous inputs and the result is validated. |
| 45 | +func TestIntSetUnmarshalJSON(t *testing.T) { |
| 46 | + testCases := []struct { |
| 47 | + data []byte |
| 48 | + expectedResult string |
| 49 | + }{ |
| 50 | + // Test to convert JSON array to set. |
| 51 | + {[]byte(`[1,2,3]`), `[1 2 3]`}, |
| 52 | + // Test to convert JSON empty array to set. |
| 53 | + {[]byte(`[]`), `[]`}, |
| 54 | + } |
| 55 | + |
| 56 | + for _, testCase := range testCases { |
| 57 | + var set IntSet |
| 58 | + set.UnmarshalJSON(testCase.data) |
| 59 | + if result := set.String(); result != testCase.expectedResult { |
| 60 | + t.Fatalf("expected: %s, got: %s", testCase.expectedResult, result) |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// IntSet.String() is called with series of cases for valid and erroneous inputs and the result is validated. |
| 66 | +func TestIntSetString(t *testing.T) { |
| 67 | + testCases := []struct { |
| 68 | + set IntSet |
| 69 | + expectedResult string |
| 70 | + }{ |
| 71 | + // Test empty set. |
| 72 | + {NewIntSet(), `[]`}, |
| 73 | + // Test set with value. |
| 74 | + {CreateIntSet(42), `[42]`}, |
| 75 | + // Test set with multiple values. |
| 76 | + {CreateIntSet(1, 2, 3), `[1 2 3]`}, |
| 77 | + } |
| 78 | + |
| 79 | + for _, testCase := range testCases { |
| 80 | + if str := testCase.set.String(); str != testCase.expectedResult { |
| 81 | + t.Fatalf("expected: %s, got: %s", testCase.expectedResult, str) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// IntSet.ToSlice() is called with series of cases for valid and erroneous inputs and the result is validated. |
| 87 | +func TestIntSetToSlice(t *testing.T) { |
| 88 | + testCases := []struct { |
| 89 | + set IntSet |
| 90 | + expectedResult []int |
| 91 | + }{ |
| 92 | + // Test empty set. |
| 93 | + {NewIntSet(), []int{}}, |
| 94 | + // Test set with value. |
| 95 | + {CreateIntSet(42), []int{42}}, |
| 96 | + // Test set with multiple values (should be sorted). |
| 97 | + {CreateIntSet(3, 1, 2), []int{1, 2, 3}}, |
| 98 | + } |
| 99 | + |
| 100 | + for _, testCase := range testCases { |
| 101 | + islice := testCase.set.ToSlice() |
| 102 | + if !reflect.DeepEqual(islice, testCase.expectedResult) { |
| 103 | + t.Fatalf("expected: %v, got: %v", testCase.expectedResult, islice) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestIntSet_UnmarshalJSON(t *testing.T) { |
| 109 | + type args struct { |
| 110 | + data []byte |
| 111 | + expectResult []int |
| 112 | + } |
| 113 | + tests := []struct { |
| 114 | + name string |
| 115 | + set IntSet |
| 116 | + args args |
| 117 | + wantErr bool |
| 118 | + }{ |
| 119 | + { |
| 120 | + name: "test ints", |
| 121 | + set: NewIntSet(), |
| 122 | + args: args{ |
| 123 | + data: []byte(`[1,2,3]`), |
| 124 | + expectResult: []int{1, 2, 3}, |
| 125 | + }, |
| 126 | + wantErr: false, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "test negative ints", |
| 130 | + set: NewIntSet(), |
| 131 | + args: args{ |
| 132 | + data: []byte(`[-1,-2,0,1,2]`), |
| 133 | + expectResult: []int{-2, -1, 0, 1, 2}, |
| 134 | + }, |
| 135 | + wantErr: false, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "test empty array", |
| 139 | + set: NewIntSet(), |
| 140 | + args: args{ |
| 141 | + data: []byte(`[]`), |
| 142 | + expectResult: []int{}, |
| 143 | + }, |
| 144 | + wantErr: false, |
| 145 | + }, |
| 146 | + } |
| 147 | + for _, tt := range tests { |
| 148 | + t.Run(tt.name, func(t *testing.T) { |
| 149 | + if err := tt.set.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { |
| 150 | + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) |
| 151 | + } |
| 152 | + slice := tt.set.ToSlice() |
| 153 | + if !reflect.DeepEqual(slice, tt.args.expectResult) { |
| 154 | + t.Errorf("IntSet() get %v, want %v", slice, tt.args.expectResult) |
| 155 | + } |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
0 commit comments