Filippo Valsorda | 37a17fe | 2018-05-08 20:09:24 -0400 | [diff] [blame] | 1 | // 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 Cox | 04dced1 | 2022-08-16 10:50:02 -0400 | [diff] [blame] | 5 | package alias |
Filippo Valsorda | 37a17fe | 2018-05-08 20:09:24 -0400 | [diff] [blame] | 6 | |
Russ Cox | 04dced1 | 2022-08-16 10:50:02 -0400 | [diff] [blame] | 7 | import "testing" |
Filippo Valsorda | 37a17fe | 2018-05-08 20:09:24 -0400 | [diff] [blame] | 8 | |
| 9 | var a, b [100]byte |
| 10 | |
| 11 | var 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 Vaghin | 7f39a6f | 2018-06-15 18:03:23 +0200 | [diff] [blame] | 26 | {a[:10], a[:10:20], true, false}, |
| 27 | {a[:10], a[5:10:20], true, true}, |
Filippo Valsorda | 37a17fe | 2018-05-08 20:09:24 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | func testAliasing(t *testing.T, i int, x, y []byte, anyOverlap, inexactOverlap bool) { |
Russ Cox | 04dced1 | 2022-08-16 10:50:02 -0400 | [diff] [blame] | 31 | any := AnyOverlap(x, y) |
Filippo Valsorda | 37a17fe | 2018-05-08 20:09:24 -0400 | [diff] [blame] | 32 | if any != anyOverlap { |
| 33 | t.Errorf("%d: wrong AnyOverlap result, expected %v, got %v", i, anyOverlap, any) |
| 34 | } |
Russ Cox | 04dced1 | 2022-08-16 10:50:02 -0400 | [diff] [blame] | 35 | inexact := InexactOverlap(x, y) |
Filippo Valsorda | 37a17fe | 2018-05-08 20:09:24 -0400 | [diff] [blame] | 36 | if inexact != inexactOverlap { |
| 37 | t.Errorf("%d: wrong InexactOverlap result, expected %v, got %v", i, inexactOverlap, any) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func 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 | } |