github.com/primecitizens/pcz/std@v0.2.1/core/mem/alias_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  //
     4  // Copyright 2018 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  package mem
     9  
    10  import "testing"
    11  
    12  var a, b [100]byte
    13  
    14  var aliasingTests = []struct {
    15  	x, y                       []byte
    16  	anyOverlap, inexactOverlap bool
    17  }{
    18  	{a[:], b[:], false, false},
    19  	{a[:], b[:0], false, false},
    20  	{a[:], b[:50], false, false},
    21  	{a[40:50], a[50:60], false, false},
    22  	{a[40:50], a[60:70], false, false},
    23  	{a[:51], a[50:], true, true},
    24  	{a[:], a[:], true, false},
    25  	{a[:50], a[:60], true, false},
    26  	{a[:], nil, false, false},
    27  	{nil, nil, false, false},
    28  	{a[:], a[:0], false, false},
    29  	{a[:10], a[:10:20], true, false},
    30  	{a[:10], a[5:10:20], true, true},
    31  }
    32  
    33  func testAliasing(t *testing.T, i int, x, y []byte, anyOverlap, inexactOverlap bool) {
    34  	any := AnyOverlap(x, y)
    35  	if any != anyOverlap {
    36  		t.Errorf("%d: wrong AnyOverlap result, expected %v, got %v", i, anyOverlap, any)
    37  	}
    38  	inexact := InexactOverlap(x, y)
    39  	if inexact != inexactOverlap {
    40  		t.Errorf("%d: wrong InexactOverlap result, expected %v, got %v", i, inexactOverlap, any)
    41  	}
    42  }
    43  
    44  func TestAliasing(t *testing.T) {
    45  	for i, tt := range aliasingTests {
    46  		testAliasing(t, i, tt.x, tt.y, tt.anyOverlap, tt.inexactOverlap)
    47  		testAliasing(t, i, tt.y, tt.x, tt.anyOverlap, tt.inexactOverlap)
    48  	}
    49  }