github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/bind/seq/string_test.go (about)

     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 seq
     6  
     7  import "testing"
     8  
     9  var strData = []string{
    10  	"abcxyz09{}",
    11  	"Hello, 世界",
    12  	string([]rune{0xffff, 0x10000, 0x10001, 0x12345, 0x10ffff}),
    13  }
    14  
    15  var stringEncoder = map[string]struct {
    16  	write func(*Buffer, string)
    17  	read  func(*Buffer) string
    18  }{
    19  	"UTF16": {write: (*Buffer).WriteUTF16, read: (*Buffer).ReadUTF16},
    20  	"UTF8":  {write: (*Buffer).WriteUTF8, read: (*Buffer).ReadUTF8},
    21  }
    22  
    23  func TestString(t *testing.T) {
    24  	for encoding, f := range stringEncoder {
    25  		for _, test := range strData {
    26  			buf := new(Buffer)
    27  			f.write(buf, test)
    28  			buf.Offset = 0
    29  			got := f.read(buf)
    30  			if got != test {
    31  				t.Errorf("%s: got %q, want %q", encoding, got, test)
    32  			}
    33  		}
    34  	}
    35  }
    36  
    37  func TestSequential(t *testing.T) {
    38  	for encoding, f := range stringEncoder {
    39  		buf := new(Buffer)
    40  		for _, test := range strData {
    41  			f.write(buf, test)
    42  		}
    43  		buf.Offset = 0
    44  		for i, test := range strData {
    45  			got := f.read(buf)
    46  			if got != test {
    47  				t.Errorf("%s: %d: got %q, want %q", encoding, i, got, test)
    48  			}
    49  		}
    50  	}
    51  }