tdo

Dicing the onion
git clone git@abtrout.com:tdo.git
Log | Files | Refs | README

bitwise_test.go (1822B)


      1 package tdo
      2 
      3 import (
      4 	"bytes"
      5 	"testing"
      6 )
      7 
      8 func TestCycleRight(t *testing.T) {
      9 	tests := []struct{ input, want byte }{
     10 		{0b00000000, 0b00000000},
     11 		{0b10101010, 0b01010101},
     12 		{0b01010101, 0b10101010},
     13 		{0b10000001, 0b11000000},
     14 		{0b11111111, 0b11111111},
     15 	}
     16 	for _, test := range tests {
     17 		if got := CycleRight(test.input); got != test.want {
     18 			t.Errorf("CycleRight(%08b) = %08b; want %08b", test.input, got, test.want)
     19 		}
     20 	}
     21 }
     22 
     23 func TestFlipSecondBits(t *testing.T) {
     24 	tests := []struct{ input, want byte }{
     25 		{0b00000000, 0b01010101},
     26 		{0b10101010, 0b11111111},
     27 		{0b10110100, 0b11100001},
     28 		{0b11111111, 0b10101010},
     29 	}
     30 	for _, test := range tests {
     31 		if got := FlipSecondBits(test.input); got != test.want {
     32 			t.Errorf("FlipSecondBits(%08b) = %08b; want %08b", test.input, got, test.want)
     33 		}
     34 	}
     35 }
     36 
     37 func TestParityCheck(t *testing.T) {
     38 	tests := []struct {
     39 		in   byte
     40 		want bool
     41 	}{
     42 		{0b10100011, true},
     43 		{0b10100010, false},
     44 
     45 		{0b11111111, true},
     46 		{0b11111110, false},
     47 
     48 		{0b00000000, true},
     49 		{0b00000001, false},
     50 	}
     51 
     52 	for _, test := range tests {
     53 		if got := ParityCheck(test.in); got != test.want {
     54 			t.Errorf("ParityCheck(%08b) = %t; want %t", test.in, got, test.want)
     55 		}
     56 	}
     57 }
     58 
     59 func TestPackDataBytes(t *testing.T) {
     60 	in := []byte{
     61 		0b11111110, 0b11111110, 0b11111110, 0b11111110,
     62 		0b11111110, 0b11111110, 0b11111110, 0b11111110,
     63 		0b11111110, 0b11111110, 0b11111110, 0b11111110,
     64 		0b11111110, 0b11111110, 0b11111110, 0b11111110,
     65 	}
     66 	want := []byte{
     67 		0b11111111, 0b11111111, 0b11111111, 0b11111111,
     68 		0b11111111, 0b11111111, 0b11111111, 0b11111111,
     69 		0b11111111, 0b11111111, 0b11111111, 0b11111111,
     70 		0b11111111, 0b11111111,
     71 	}
     72 	if got := PackDataBytes(in); !bytes.Equal(got, want) {
     73 		t.Errorf("PackDataBytes(%#v) = %#v (len %d); want %#v (len %d)", in, got, len(got), want, len(want))
     74 	}
     75 }