From: Dave Cheney Date: Mon, 16 Dec 2013 01:35:25 +0000 (+1100) Subject: unicode/utf16: add explicit test for decoding invalid runes. X-Git-Tag: go1.3beta1~1232 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=62baae6e57ca9271fc9a4269958d474aa398cc00;p=gostls13.git unicode/utf16: add explicit test for decoding invalid runes. The EncodeRune test exercises DecodeRune, but only for runes that it can encode. Add an explicit test for invalid utf16 surrogate pairs. Bonus: coverage is now 100% unicode/utf16/utf16.go: IsSurrogate 100.0% unicode/utf16/utf16.go: DecodeRune 100.0% unicode/utf16/utf16.go: EncodeRune 100.0% unicode/utf16/utf16.go: Encode 100.0% unicode/utf16/utf16.go: Decode 100.0% total: (statements) 100.0% R=golang-dev, r CC=golang-dev https://golang.org/cl/39150044 --- diff --git a/src/pkg/unicode/utf16/utf16_test.go b/src/pkg/unicode/utf16/utf16_test.go index 05d6427b05..3dca472bbe 100644 --- a/src/pkg/unicode/utf16/utf16_test.go +++ b/src/pkg/unicode/utf16/utf16_test.go @@ -100,6 +100,26 @@ func TestDecode(t *testing.T) { } } +var decodeRuneTests = []struct { + r1, r2 rune + want rune +}{ + {0xd800, 0xdc00, 0x10000}, + {0xd800, 0xdc01, 0x10001}, + {0xd808, 0xdf45, 0x12345}, + {0xdbff, 0xdfff, 0x10ffff}, + {0xd800, 'a', 0xfffd}, // illegal, replacement rune substituted +} + +func TestDecodeRune(t *testing.T) { + for i, tt := range decodeRuneTests { + got := DecodeRune(tt.r1, tt.r2) + if got != tt.want { + t.Errorf("%d: DecodeRune(%q, %q) = %v; want %v", i, tt.r1, tt.r2, got, tt.want) + } + } +} + var surrogateTests = []struct { r rune want bool