| 1 | package interfaces |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestEqualFoldASCII(t *testing.T) { |
| 6 | cases := []struct { |
| 7 | s, t string |
| 8 | want bool |
| 9 | }{ |
| 10 | {"A", "A", true}, |
| 11 | {"A", "a", true}, |
| 12 | {"a", "A", true}, |
| 13 | {"ACG", "acg", true}, |
| 14 | {"acg", "ACG", true}, |
| 15 | {"ACG", "ACG", true}, |
| 16 | {"ACG", "ACT", false}, |
| 17 | {"ACG", "AC", false}, |
| 18 | {"AC", "ACG", false}, |
| 19 | {"", "", true}, |
| 20 | // Non-letter bytes that differ by 0x20 must not fold. |
| 21 | // '0' (0x30) ^ 0x10 (0x10) = 0x20, but neither is a letter. |
| 22 | {"0", "\x10", false}, |
| 23 | // Digits are equal only when identical. |
| 24 | {"1", "1", true}, |
| 25 | {"1", "2", false}, |
| 26 | } |
| 27 | for _, tc := range cases { |
| 28 | if got := equalFoldASCII(tc.s, tc.t); got != tc.want { |
| 29 | t.Errorf("equalFoldASCII(%q, %q) = %v, want %v", tc.s, tc.t, got, tc.want) |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | type mockRefAlt struct { |
| 35 | chrom string |
| 36 | start uint32 |
| 37 | end uint32 |
| 38 | ref string |
| 39 | alt []string |
| 40 | } |
| 41 | |
| 42 | func (m mockRefAlt) Chrom() string { return m.chrom } |
| 43 | func (m mockRefAlt) Start() uint32 { return m.start } |
| 44 | func (m mockRefAlt) End() uint32 { return m.end } |
| 45 | func (m mockRefAlt) Ref() string { return m.ref } |
| 46 | func (m mockRefAlt) Alt() []string { return m.alt } |
| 47 | |
| 48 | func TestSameVariant(t *testing.T) { |
| 49 | base := mockRefAlt{"chr1", 100, 101, "A", []string{"T"}} |
| 50 | |
| 51 | cases := []struct { |
| 52 | desc string |
| 53 | a, b mockRefAlt |
| 54 | want bool |
| 55 | }{ |
| 56 | { |
| 57 | desc: "identical uppercase", |
| 58 | a: base, |
| 59 | b: base, |
| 60 | want: true, |
| 61 | }, |
| 62 | { |
| 63 | desc: "ref lowercase vs uppercase", |
| 64 | a: mockRefAlt{"chr1", 100, 101, "a", []string{"T"}}, |
| 65 | b: base, |
| 66 | want: true, |
| 67 | }, |
| 68 | { |
| 69 | desc: "alt lowercase vs uppercase", |
| 70 | a: mockRefAlt{"chr1", 100, 101, "A", []string{"t"}}, |
| 71 | b: base, |
| 72 | want: true, |
| 73 | }, |
| 74 | { |
| 75 | desc: "both ref and alt lowercase", |
| 76 | a: mockRefAlt{"chr1", 100, 101, "a", []string{"t"}}, |
| 77 | b: base, |
| 78 | want: true, |
| 79 | }, |
| 80 | { |
| 81 | desc: "different ref", |
| 82 | a: mockRefAlt{"chr1", 100, 101, "C", []string{"T"}}, |
| 83 | b: base, |
| 84 | want: false, |
| 85 | }, |
| 86 | { |
| 87 | desc: "different alt", |
| 88 | a: mockRefAlt{"chr1", 100, 101, "A", []string{"G"}}, |
| 89 | b: base, |
| 90 | want: false, |
| 91 | }, |
| 92 | { |
| 93 | desc: "different position", |
| 94 | a: mockRefAlt{"chr1", 200, 201, "A", []string{"T"}}, |
| 95 | b: base, |
| 96 | want: false, |
| 97 | }, |
| 98 | { |
| 99 | desc: "different chrom", |
| 100 | a: mockRefAlt{"chr2", 100, 101, "A", []string{"T"}}, |
| 101 | b: base, |
| 102 | want: false, |
| 103 | }, |
| 104 | { |
| 105 | desc: "NON_REF on left matches any ref-matching variant", |
| 106 | a: mockRefAlt{"chr1", 100, 101, "A", []string{"<NON_REF>"}}, |
| 107 | b: base, |
| 108 | want: true, |
| 109 | }, |
| 110 | { |
| 111 | desc: "NON_REF on right matches any ref-matching variant", |
| 112 | a: base, |
| 113 | b: mockRefAlt{"chr1", 100, 101, "A", []string{"<NON_REF>"}}, |
| 114 | want: true, |
| 115 | }, |
| 116 | { |
| 117 | desc: "multi-alt: one matching alt (mixed case)", |
| 118 | a: mockRefAlt{"chr1", 100, 101, "A", []string{"G", "t"}}, |
| 119 | b: mockRefAlt{"chr1", 100, 101, "A", []string{"T", "C"}}, |
| 120 | want: true, |
| 121 | }, |
| 122 | { |
| 123 | desc: "multi-alt: no matching alt", |
| 124 | a: mockRefAlt{"chr1", 100, 101, "A", []string{"G", "C"}}, |
| 125 | b: mockRefAlt{"chr1", 100, 101, "A", []string{"T"}}, |
| 126 | want: false, |
| 127 | }, |
| 128 | { |
| 129 | desc: "empty alt on left", |
| 130 | a: mockRefAlt{"chr1", 100, 101, "A", []string{}}, |
| 131 | b: base, |
| 132 | want: false, |
| 133 | }, |
| 134 | { |
| 135 | desc: "empty alt on right", |
| 136 | a: base, |
| 137 | b: mockRefAlt{"chr1", 100, 101, "A", []string{}}, |
| 138 | want: false, |
| 139 | }, |
| 140 | } |
| 141 | |
| 142 | for _, tc := range cases { |
| 143 | t.Run(tc.desc, func(t *testing.T) { |
| 144 | if got := SameVariant(tc.a, tc.b); got != tc.want { |
| 145 | t.Errorf("SameVariant: got %v, want %v", got, tc.want) |
| 146 | } |
| 147 | }) |
| 148 | } |
| 149 | } |