github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/unicode/utf16/utf16_test.go (about)

     1  // Copyright 2010 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 utf16_test
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  	"unicode"
    11  	. "unicode/utf16"
    12  )
    13  
    14  // Validate the constants redefined from unicode.
    15  func TestConstants(t *testing.T) {
    16  	if MaxRune != unicode.MaxRune {
    17  		t.Errorf("utf16.maxRune is wrong: %x should be %x", MaxRune, unicode.MaxRune)
    18  	}
    19  	if ReplacementChar != unicode.ReplacementChar {
    20  		t.Errorf("utf16.replacementChar is wrong: %x should be %x", ReplacementChar, unicode.ReplacementChar)
    21  	}
    22  }
    23  
    24  type encodeTest struct {
    25  	in  []rune
    26  	out []uint16
    27  }
    28  
    29  var encodeTests = []encodeTest{
    30  	{[]rune{1, 2, 3, 4}, []uint16{1, 2, 3, 4}},
    31  	{[]rune{0xffff, 0x10000, 0x10001, 0x12345, 0x10ffff},
    32  		[]uint16{0xffff, 0xd800, 0xdc00, 0xd800, 0xdc01, 0xd808, 0xdf45, 0xdbff, 0xdfff}},
    33  	{[]rune{'a', 'b', 0xd7ff, 0xd800, 0xdfff, 0xe000, 0x110000, -1},
    34  		[]uint16{'a', 'b', 0xd7ff, 0xfffd, 0xfffd, 0xe000, 0xfffd, 0xfffd}},
    35  }
    36  
    37  func TestEncode(t *testing.T) {
    38  	for _, tt := range encodeTests {
    39  		out := Encode(tt.in)
    40  		if !reflect.DeepEqual(out, tt.out) {
    41  			t.Errorf("Encode(%x) = %x; want %x", tt.in, out, tt.out)
    42  		}
    43  	}
    44  }
    45  
    46  func TestEncodeRune(t *testing.T) {
    47  	for i, tt := range encodeTests {
    48  		j := 0
    49  		for _, r := range tt.in {
    50  			r1, r2 := EncodeRune(r)
    51  			if r < 0x10000 || r > unicode.MaxRune {
    52  				if j >= len(tt.out) {
    53  					t.Errorf("#%d: ran out of tt.out", i)
    54  					break
    55  				}
    56  				if r1 != unicode.ReplacementChar || r2 != unicode.ReplacementChar {
    57  					t.Errorf("EncodeRune(%#x) = %#x, %#x; want 0xfffd, 0xfffd", r, r1, r2)
    58  				}
    59  				j++
    60  			} else {
    61  				if j+1 >= len(tt.out) {
    62  					t.Errorf("#%d: ran out of tt.out", i)
    63  					break
    64  				}
    65  				if r1 != rune(tt.out[j]) || r2 != rune(tt.out[j+1]) {
    66  					t.Errorf("EncodeRune(%#x) = %#x, %#x; want %#x, %#x", r, r1, r2, tt.out[j], tt.out[j+1])
    67  				}
    68  				j += 2
    69  				dec := DecodeRune(r1, r2)
    70  				if dec != r {
    71  					t.Errorf("DecodeRune(%#x, %#x) = %#x; want %#x", r1, r2, dec, r)
    72  				}
    73  			}
    74  		}
    75  		if j != len(tt.out) {
    76  			t.Errorf("#%d: EncodeRune didn't generate enough output", i)
    77  		}
    78  	}
    79  }
    80  
    81  type decodeTest struct {
    82  	in  []uint16
    83  	out []rune
    84  }
    85  
    86  var decodeTests = []decodeTest{
    87  	{[]uint16{1, 2, 3, 4}, []rune{1, 2, 3, 4}},
    88  	{[]uint16{0xffff, 0xd800, 0xdc00, 0xd800, 0xdc01, 0xd808, 0xdf45, 0xdbff, 0xdfff},
    89  		[]rune{0xffff, 0x10000, 0x10001, 0x12345, 0x10ffff}},
    90  	{[]uint16{0xd800, 'a'}, []rune{0xfffd, 'a'}},
    91  	{[]uint16{0xdfff}, []rune{0xfffd}},
    92  }
    93  
    94  func TestDecode(t *testing.T) {
    95  	for _, tt := range decodeTests {
    96  		out := Decode(tt.in)
    97  		if !reflect.DeepEqual(out, tt.out) {
    98  			t.Errorf("Decode(%x) = %x; want %x", tt.in, out, tt.out)
    99  		}
   100  	}
   101  }