github.com/philpearl/plenc@v0.0.15/plenccodec/bool_test.go (about)

     1  package plenccodec_test
     2  
     3  import (
     4  	"testing"
     5  	"unsafe"
     6  
     7  	"github.com/philpearl/plenc"
     8  	"github.com/philpearl/plenc/plenccodec"
     9  )
    10  
    11  func TestBool(t *testing.T) {
    12  	c := plenccodec.BoolCodec{}
    13  	for _, b := range []bool{false, true} {
    14  		s := c.Size(unsafe.Pointer(&b), nil)
    15  		data := c.Append(nil, unsafe.Pointer(&b), nil)
    16  		var a bool
    17  		n, err := c.Read(data, unsafe.Pointer(&a), c.WireType())
    18  		if err != nil {
    19  			t.Fatal(err)
    20  		}
    21  		if s != n {
    22  			t.Errorf("size mismatch %d %d", s, n)
    23  		}
    24  		if a != b {
    25  			t.Errorf("Result incorrect for %t", b)
    26  		}
    27  	}
    28  }
    29  
    30  func TestBoolMarshal(t *testing.T) {
    31  	for _, b := range []bool{false, true} {
    32  		data, err := plenc.Marshal(nil, &b)
    33  		if err != nil {
    34  			t.Fatal(err)
    35  		}
    36  		var out bool
    37  		if err := plenc.Unmarshal(data, &out); err != nil {
    38  			t.Fatal(err)
    39  		}
    40  		if b != out {
    41  			t.Errorf("Result incorrect for %t", b)
    42  		}
    43  	}
    44  }