]> Cypherpunks repositories - gostls13.git/commitdiff
unicode/utf16: add explicit test for decoding invalid runes.
authorDave Cheney <dave@cheney.net>
Mon, 16 Dec 2013 01:35:25 +0000 (12:35 +1100)
committerDave Cheney <dave@cheney.net>
Mon, 16 Dec 2013 01:35:25 +0000 (12:35 +1100)
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

src/pkg/unicode/utf16/utf16_test.go

index 05d6427b05f9363d6152716094ad2ccd676ded4a..3dca472bbe2165197fa4fb213adade90ff5bf930 100644 (file)
@@ -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