blob: a68fb33667bf633067dbef222387f230ca51a918 [file] [log] [blame]
Filippo Valsorda37a17fe2018-05-08 20:09:24 -04001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Russ Cox04dced12022-08-16 10:50:02 -04005package alias
Filippo Valsorda37a17fe2018-05-08 20:09:24 -04006
Russ Cox04dced12022-08-16 10:50:02 -04007import "testing"
Filippo Valsorda37a17fe2018-05-08 20:09:24 -04008
9var a, b [100]byte
10
11var aliasingTests = []struct {
12 x, y []byte
13 anyOverlap, inexactOverlap bool
14}{
15 {a[:], b[:], false, false},
16 {a[:], b[:0], false, false},
17 {a[:], b[:50], false, false},
18 {a[40:50], a[50:60], false, false},
19 {a[40:50], a[60:70], false, false},
20 {a[:51], a[50:], true, true},
21 {a[:], a[:], true, false},
22 {a[:50], a[:60], true, false},
23 {a[:], nil, false, false},
24 {nil, nil, false, false},
25 {a[:], a[:0], false, false},
Alex Vaghin7f39a6f2018-06-15 18:03:23 +020026 {a[:10], a[:10:20], true, false},
27 {a[:10], a[5:10:20], true, true},
Filippo Valsorda37a17fe2018-05-08 20:09:24 -040028}
29
30func testAliasing(t *testing.T, i int, x, y []byte, anyOverlap, inexactOverlap bool) {
Russ Cox04dced12022-08-16 10:50:02 -040031 any := AnyOverlap(x, y)
Filippo Valsorda37a17fe2018-05-08 20:09:24 -040032 if any != anyOverlap {
33 t.Errorf("%d: wrong AnyOverlap result, expected %v, got %v", i, anyOverlap, any)
34 }
Russ Cox04dced12022-08-16 10:50:02 -040035 inexact := InexactOverlap(x, y)
Filippo Valsorda37a17fe2018-05-08 20:09:24 -040036 if inexact != inexactOverlap {
37 t.Errorf("%d: wrong InexactOverlap result, expected %v, got %v", i, inexactOverlap, any)
38 }
39}
40
41func TestAliasing(t *testing.T) {
42 for i, tt := range aliasingTests {
43 testAliasing(t, i, tt.x, tt.y, tt.anyOverlap, tt.inexactOverlap)
44 testAliasing(t, i, tt.y, tt.x, tt.anyOverlap, tt.inexactOverlap)
45 }
46}