gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/hostarch/addr_test.go (about)

     1  // Copyright 2022 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package hostarch
    16  
    17  import (
    18  	"fmt"
    19  	"math"
    20  	"testing"
    21  )
    22  
    23  func TestCacheLineRoundUp(t *testing.T) {
    24  	tests := []struct {
    25  		input  uint64
    26  		output uint64
    27  	}{
    28  		{0, 0},
    29  		{63, 64},
    30  		{64, 64},
    31  		{65, 128},
    32  		{66, 128},
    33  		{99, 128},
    34  		{127, 128},
    35  		{128, 128},
    36  		{129, 192},
    37  		{math.MaxUint32, math.MaxUint32 + 1},
    38  	}
    39  	for i, tt := range tests {
    40  		t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
    41  			s, ok := CacheLineRoundUp(tt.input)
    42  			if s != tt.output {
    43  				t.Errorf("Expected %d, got %d", tt.output, s)
    44  			}
    45  			if !ok {
    46  				t.Errorf("Expected no wrap around, got %t. Input %d, expected %d", ok, tt.input, tt.output)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestCacheLineRoundUpWrapAround(t *testing.T) {
    53  	tests := []struct {
    54  		input  uint64
    55  		output uint64
    56  	}{
    57  		{math.MaxUint64 - 1, 0},
    58  		{math.MaxUint64, 0},
    59  	}
    60  	for i, tt := range tests {
    61  		t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
    62  			s, ok := CacheLineRoundUp(tt.input)
    63  			if s != tt.output {
    64  				t.Errorf("Expected %d, got %d", tt.output, s)
    65  			}
    66  			if ok {
    67  				t.Errorf("Expected wrap around, got %t. Input %d, expected %d", ok, tt.input, tt.output)
    68  			}
    69  		})
    70  	}
    71  }