github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/state/tests/integer_test.go (about)

     1  // Copyright 2018 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 tests
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  )
    21  
    22  var (
    23  	allBasicInts  = []int{-1, 0, 1}
    24  	allInt8s      = []int8{math.MinInt8, -1, 0, 1, math.MaxInt8}
    25  	allInt16s     = []int16{math.MinInt16, -1, 0, 1, math.MaxInt16}
    26  	allInt32s     = []int32{math.MinInt32, -1, 0, 1, math.MaxInt32}
    27  	allInt64s     = []int64{math.MinInt64, -1, 0, 1, math.MaxInt64}
    28  	allBasicUints = []uint{0, 1}
    29  	allUintptrs   = []uintptr{0, 1, ^uintptr(0)}
    30  	allUint8s     = []uint8{0, 1, math.MaxUint8}
    31  	allUint16s    = []uint16{0, 1, math.MaxUint16}
    32  	allUint32s    = []uint32{0, 1, math.MaxUint32}
    33  	allUint64s    = []uint64{0, 1, math.MaxUint64}
    34  )
    35  
    36  var allInts = flatten(
    37  	allBasicInts,
    38  	allInt8s,
    39  	allInt16s,
    40  	allInt32s,
    41  	allInt64s,
    42  )
    43  
    44  var allUints = flatten(
    45  	allBasicUints,
    46  	allUintptrs,
    47  	allUint8s,
    48  	allUint16s,
    49  	allUint32s,
    50  	allUint64s,
    51  )
    52  
    53  func TestInt(t *testing.T) {
    54  	runTestCases(t, false, "plain", allInts)
    55  	runTestCases(t, false, "pointers", pointersTo(allInts))
    56  	runTestCases(t, false, "interfaces", interfacesTo(allInts))
    57  	runTestCases(t, false, "interfacesTo", interfacesTo(pointersTo(allInts)))
    58  }
    59  
    60  func TestIntTruncation(t *testing.T) {
    61  	runTestCases(t, true, "pass", []interface{}{
    62  		truncatingInt8{save: math.MinInt8 - 1},
    63  		truncatingInt16{save: math.MinInt16 - 1},
    64  		truncatingInt32{save: math.MinInt32 - 1},
    65  		truncatingInt8{save: math.MaxInt8 + 1},
    66  		truncatingInt16{save: math.MaxInt16 + 1},
    67  		truncatingInt32{save: math.MaxInt32 + 1},
    68  	})
    69  	runTestCases(t, false, "fail", []interface{}{
    70  		truncatingInt8{save: 1},
    71  		truncatingInt16{save: 1},
    72  		truncatingInt32{save: 1},
    73  	})
    74  }
    75  
    76  func TestUint(t *testing.T) {
    77  	runTestCases(t, false, "plain", allUints)
    78  	runTestCases(t, false, "pointers", pointersTo(allUints))
    79  	runTestCases(t, false, "interfaces", interfacesTo(allUints))
    80  	runTestCases(t, false, "interfacesTo", interfacesTo(pointersTo(allUints)))
    81  }
    82  
    83  func TestUintTruncation(t *testing.T) {
    84  	runTestCases(t, true, "pass", []interface{}{
    85  		truncatingUint8{save: math.MaxUint8 + 1},
    86  		truncatingUint16{save: math.MaxUint16 + 1},
    87  		truncatingUint32{save: math.MaxUint32 + 1},
    88  	})
    89  	runTestCases(t, false, "fail", []interface{}{
    90  		truncatingUint8{save: 1},
    91  		truncatingUint16{save: 1},
    92  		truncatingUint32{save: 1},
    93  	})
    94  }