...

Source file src/golang.org/x/arch/ppc64/ppc64asm/field_test.go

Documentation: golang.org/x/arch/ppc64/ppc64asm

     1  // Copyright 2014 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  
     5  package ppc64asm
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func panicOrNot(f func()) (panicked bool) {
    12  	defer func() {
    13  		if err := recover(); err != nil {
    14  			panicked = true
    15  		}
    16  	}()
    17  	f()
    18  	return false
    19  }
    20  
    21  func TestBitField(t *testing.T) {
    22  	var tests = []struct {
    23  		b    BitField
    24  		i    uint32 // input
    25  		u    uint32 // unsigned output
    26  		s    int32  // signed output
    27  		fail bool   // if the check should panic
    28  	}{
    29  		{BitField{0, 0, 0}, 0, 0, 0, true},
    30  		{BitField{31, 2, 0}, 0, 0, 0, true},
    31  		{BitField{31, 1, 0}, 1, 1, -1, false},
    32  		{BitField{29, 2, 0}, 0 << 1, 0, 0, false},
    33  		{BitField{29, 2, 0}, 1 << 1, 1, 1, false},
    34  		{BitField{29, 2, 0}, 2 << 1, 2, -2, false},
    35  		{BitField{29, 2, 0}, 3 << 1, 3, -1, false},
    36  		{BitField{0, 32, 0}, 1<<32 - 1, 1<<32 - 1, -1, false},
    37  		{BitField{16, 3, 0}, 1 << 15, 4, -4, false},
    38  	}
    39  	for i, tst := range tests {
    40  		var (
    41  			ou uint32
    42  			os int32
    43  		)
    44  		failed := panicOrNot(func() {
    45  			ou = tst.b.Parse([2]uint32{tst.i})
    46  			os = tst.b.ParseSigned([2]uint32{tst.i})
    47  		})
    48  		if failed != tst.fail {
    49  			t.Errorf("case %d: %v: fail test failed, got %v, expected %v", i, tst.b, failed, tst.fail)
    50  			continue
    51  		}
    52  		if ou != tst.u {
    53  			t.Errorf("case %d: %v.Parse(%d) returned %d, expected %d", i, tst.b, tst.i, ou, tst.u)
    54  			continue
    55  		}
    56  		if os != tst.s {
    57  			t.Errorf("case %d: %v.ParseSigned(%d) returned %d, expected %d", i, tst.b, tst.i, os, tst.s)
    58  		}
    59  	}
    60  }
    61  
    62  func TestBitFields(t *testing.T) {
    63  	var tests = []struct {
    64  		b    BitFields
    65  		i    [2]uint32 // input
    66  		u    uint64    // unsigned output
    67  		s    int64     // signed output
    68  		nb   int       // Total number of bits in BitField
    69  		fail bool      // if the check should panic
    70  	}{
    71  		{BitFields{{0, 0, 1}}, [2]uint32{0, 0}, 0, 0, 0, true},
    72  		{BitFields{{31, 2, 1}}, [2]uint32{0, 0}, 0, 0, 2, true},
    73  		{BitFields{{31, 1, 1}}, [2]uint32{0, 1}, 1, -1, 1, false},
    74  		{BitFields{{29, 2, 1}}, [2]uint32{0, 0 << 1}, 0, 0, 2, false},
    75  		{BitFields{{29, 2, 1}}, [2]uint32{0, 1 << 1}, 1, 1, 2, false},
    76  		{BitFields{{29, 2, 1}}, [2]uint32{0, 2 << 1}, 2, -2, 2, false},
    77  		{BitFields{{29, 2, 1}}, [2]uint32{0, 3 << 1}, 3, -1, 2, false},
    78  		{BitFields{{0, 32, 1}}, [2]uint32{0, 1<<32 - 1}, 1<<32 - 1, -1, 32, false},
    79  		{BitFields{{16, 3, 1}}, [2]uint32{0, 1 << 15}, 4, -4, 3, false},
    80  		{BitFields{{16, 16, 0}, {16, 16, 1}}, [2]uint32{0x8016, 0x32}, 0x80160032, -0x7FE9FFCE, 32, false},
    81  		{BitFields{{14, 18, 0}, {16, 16, 1}}, [2]uint32{0x38016, 0x32}, 0x380160032, -0x07FE9FFCE, 34, false},
    82  	}
    83  	for i, tst := range tests {
    84  		var (
    85  			ou  uint64
    86  			os  int64
    87  			onb int
    88  		)
    89  		failed := panicOrNot(func() {
    90  			onb = tst.b.NumBits()
    91  			ou = tst.b.Parse(tst.i)
    92  			os = tst.b.ParseSigned(tst.i)
    93  		})
    94  		if failed != tst.fail {
    95  			t.Errorf("case %d: %v: fail test failed, got %v, expected %v", i, tst.b, failed, tst.fail)
    96  			continue
    97  		}
    98  		if ou != tst.u {
    99  			t.Errorf("case %d: %v.Parse(%d) returned %d, expected %d", i, tst.b, tst.i, ou, tst.u)
   100  			continue
   101  		}
   102  		if os != tst.s {
   103  			t.Errorf("case %d: %v.ParseSigned(%d) returned %d, expected %d", i, tst.b, tst.i, os, tst.s)
   104  		}
   105  		if onb != tst.nb {
   106  			t.Errorf("case %d: %v.NumBits() returned %d, expected %d", i, tst.b, onb, tst.nb)
   107  		}
   108  	}
   109  }
   110  

View as plain text