|
| 1 | +//nolint:revive |
| 2 | +package util_test |
| 3 | + |
| 4 | +import ( |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/babylonlabs-io/finality-provider/util" |
| 10 | +) |
| 11 | + |
| 12 | +func TestHasDuplicateHeights(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + heights []uint64 |
| 18 | + expectDup bool |
| 19 | + dupHeight uint64 |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "no duplicates", |
| 23 | + heights: []uint64{1, 2, 3, 4, 5}, |
| 24 | + expectDup: false, |
| 25 | + dupHeight: 0, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "duplicate at start", |
| 29 | + heights: []uint64{1, 1, 3}, |
| 30 | + expectDup: true, |
| 31 | + dupHeight: 1, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "duplicate in middle", |
| 35 | + heights: []uint64{1, 2, 2, 3}, |
| 36 | + expectDup: true, |
| 37 | + dupHeight: 2, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "duplicate at end", |
| 41 | + heights: []uint64{1, 2, 3, 3}, |
| 42 | + expectDup: true, |
| 43 | + dupHeight: 3, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "empty slice", |
| 47 | + heights: []uint64{}, |
| 48 | + expectDup: false, |
| 49 | + dupHeight: 0, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "single element", |
| 53 | + heights: []uint64{42}, |
| 54 | + expectDup: false, |
| 55 | + dupHeight: 0, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "two identical elements", |
| 59 | + heights: []uint64{100, 100}, |
| 60 | + expectDup: true, |
| 61 | + dupHeight: 100, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "non-consecutive unique heights", |
| 65 | + heights: []uint64{10, 20, 30, 15, 25}, |
| 66 | + expectDup: false, |
| 67 | + dupHeight: 0, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "large numbers no duplicates", |
| 71 | + heights: []uint64{1000000, 2000000, 3000000}, |
| 72 | + expectDup: false, |
| 73 | + dupHeight: 0, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "large numbers with duplicate", |
| 77 | + heights: []uint64{1000000, 2000000, 1000000}, |
| 78 | + expectDup: true, |
| 79 | + dupHeight: 1000000, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + |
| 87 | + hasDup, dupHeight := util.HasDuplicateHeights(tt.heights) |
| 88 | + require.Equal(t, tt.expectDup, hasDup) |
| 89 | + if tt.expectDup { |
| 90 | + require.Equal(t, tt.dupHeight, dupHeight) |
| 91 | + } else { |
| 92 | + require.Equal(t, uint64(0), dupHeight) |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestValidateNoDuplicateHeights(t *testing.T) { |
| 99 | + t.Parallel() |
| 100 | + |
| 101 | + tests := []struct { |
| 102 | + name string |
| 103 | + heights []uint64 |
| 104 | + expectErr bool |
| 105 | + }{ |
| 106 | + { |
| 107 | + name: "valid unique heights", |
| 108 | + heights: []uint64{1, 2, 3, 4, 5}, |
| 109 | + expectErr: false, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "invalid with duplicates", |
| 113 | + heights: []uint64{1, 2, 2, 3}, |
| 114 | + expectErr: true, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "empty is valid", |
| 118 | + heights: []uint64{}, |
| 119 | + expectErr: false, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "single element is valid", |
| 123 | + heights: []uint64{1}, |
| 124 | + expectErr: false, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + for _, tt := range tests { |
| 129 | + t.Run(tt.name, func(t *testing.T) { |
| 130 | + t.Parallel() |
| 131 | + |
| 132 | + err := util.ValidateNoDuplicateHeights(tt.heights) |
| 133 | + if tt.expectErr { |
| 134 | + require.Error(t, err) |
| 135 | + require.Contains(t, err.Error(), "duplicate height detected") |
| 136 | + } else { |
| 137 | + require.NoError(t, err) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments