Home | History | Annotate | Download | only in 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, true},
     30 		{BitField{31, 2}, 0, 0, 0, true},
     31 		{BitField{31, 1}, 1, 1, -1, false},
     32 		{BitField{29, 2}, 0 << 1, 0, 0, false},
     33 		{BitField{29, 2}, 1 << 1, 1, 1, false},
     34 		{BitField{29, 2}, 2 << 1, 2, -2, false},
     35 		{BitField{29, 2}, 3 << 1, 3, -1, false},
     36 		{BitField{0, 32}, 1<<32 - 1, 1<<32 - 1, -1, false},
     37 		{BitField{16, 3}, 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(tst.i)
     46 			os = tst.b.ParseSigned(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