| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | // Copyright 2009 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | package utf8_test | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | 		"bytes"; | 
					
						
							|  |  |  | 		"strings"; | 
					
						
							|  |  |  | 		"testing"; | 
					
						
							|  |  |  | 	.	"utf8"; | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-20 14:40:40 -08:00
										 |  |  | type Utf8Map struct { | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | 	rune	int; | 
					
						
							|  |  |  | 	str	string; | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | var utf8map = []Utf8Map{ | 
					
						
							|  |  |  | 	Utf8Map{0x0000, "\x00"}, | 
					
						
							|  |  |  | 	Utf8Map{0x0001, "\x01"}, | 
					
						
							|  |  |  | 	Utf8Map{0x007e, "\x7e"}, | 
					
						
							|  |  |  | 	Utf8Map{0x007f, "\x7f"}, | 
					
						
							|  |  |  | 	Utf8Map{0x0080, "\xc2\x80"}, | 
					
						
							|  |  |  | 	Utf8Map{0x0081, "\xc2\x81"}, | 
					
						
							|  |  |  | 	Utf8Map{0x00bf, "\xc2\xbf"}, | 
					
						
							|  |  |  | 	Utf8Map{0x00c0, "\xc3\x80"}, | 
					
						
							|  |  |  | 	Utf8Map{0x00c1, "\xc3\x81"}, | 
					
						
							|  |  |  | 	Utf8Map{0x00c8, "\xc3\x88"}, | 
					
						
							|  |  |  | 	Utf8Map{0x00d0, "\xc3\x90"}, | 
					
						
							|  |  |  | 	Utf8Map{0x00e0, "\xc3\xa0"}, | 
					
						
							|  |  |  | 	Utf8Map{0x00f0, "\xc3\xb0"}, | 
					
						
							|  |  |  | 	Utf8Map{0x00f8, "\xc3\xb8"}, | 
					
						
							|  |  |  | 	Utf8Map{0x00ff, "\xc3\xbf"}, | 
					
						
							|  |  |  | 	Utf8Map{0x0100, "\xc4\x80"}, | 
					
						
							|  |  |  | 	Utf8Map{0x07ff, "\xdf\xbf"}, | 
					
						
							|  |  |  | 	Utf8Map{0x0800, "\xe0\xa0\x80"}, | 
					
						
							|  |  |  | 	Utf8Map{0x0801, "\xe0\xa0\x81"}, | 
					
						
							|  |  |  | 	Utf8Map{0xfffe, "\xef\xbf\xbe"}, | 
					
						
							|  |  |  | 	Utf8Map{0xffff, "\xef\xbf\xbf"}, | 
					
						
							|  |  |  | 	Utf8Map{0x10000, "\xf0\x90\x80\x80"}, | 
					
						
							|  |  |  | 	Utf8Map{0x10001, "\xf0\x90\x80\x81"}, | 
					
						
							|  |  |  | 	Utf8Map{0x10fffe, "\xf4\x8f\xbf\xbe"}, | 
					
						
							|  |  |  | 	Utf8Map{0x10ffff, "\xf4\x8f\xbf\xbf"}, | 
					
						
							| 
									
										
										
										
											2009-03-03 08:39:12 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-29 15:24:23 -07:00
										 |  |  | // strings.Bytes with one extra byte at end | 
					
						
							| 
									
										
										
										
											2009-06-04 15:28:09 -07:00
										 |  |  | func makeBytes(s string) []byte { | 
					
						
							| 
									
										
										
										
											2009-02-06 17:54:26 -08:00
										 |  |  | 	s += "\x00"; | 
					
						
							| 
									
										
										
										
											2009-06-29 15:24:23 -07:00
										 |  |  | 	b := strings.Bytes(s); | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | 	return b[0 : len(s)-1]; | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-20 14:40:40 -08:00
										 |  |  | func TestFullRune(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 	for i := 0; i < len(utf8map); i++ { | 
					
						
							|  |  |  | 		m := utf8map[i]; | 
					
						
							| 
									
										
										
										
											2009-06-04 15:28:09 -07:00
										 |  |  | 		b := makeBytes(m.str); | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		if !FullRune(b) { | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 			t.Errorf("FullRune(%q) (rune %04x) = false, want true", b, m.rune); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-05-11 14:10:34 -07:00
										 |  |  | 		s := m.str; | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		if !FullRuneInString(s) { | 
					
						
							| 
									
										
										
										
											2009-05-11 14:10:34 -07:00
										 |  |  | 			t.Errorf("FullRuneInString(%q) (rune %04x) = false, want true", s, m.rune); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | 		b1 := b[0 : len(b)-1]; | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		if FullRune(b1) { | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 			t.Errorf("FullRune(%q) = true, want false", b1); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-05-11 14:10:34 -07:00
										 |  |  | 		s1 := string(b1); | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		if FullRuneInString(s1) { | 
					
						
							| 
									
										
										
										
											2009-05-11 14:10:34 -07:00
										 |  |  | 			t.Errorf("FullRune(%q) = true, want false", s1); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-20 14:40:40 -08:00
										 |  |  | func TestEncodeRune(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 	for i := 0; i < len(utf8map); i++ { | 
					
						
							|  |  |  | 		m := utf8map[i]; | 
					
						
							| 
									
										
										
										
											2009-06-04 15:28:09 -07:00
										 |  |  | 		b := makeBytes(m.str); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		var buf [10]byte; | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		n := EncodeRune(m.rune, &buf); | 
					
						
							| 
									
										
										
										
											2008-12-18 22:37:22 -08:00
										 |  |  | 		b1 := buf[0:n]; | 
					
						
							| 
									
										
										
										
											2009-06-04 15:28:09 -07:00
										 |  |  | 		if !bytes.Equal(b, b1) { | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 			t.Errorf("EncodeRune(0x%04x) = %q want %q", m.rune, b1, b); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-20 14:40:40 -08:00
										 |  |  | func TestDecodeRune(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 	for i := 0; i < len(utf8map); i++ { | 
					
						
							|  |  |  | 		m := utf8map[i]; | 
					
						
							| 
									
										
										
										
											2009-06-04 15:28:09 -07:00
										 |  |  | 		b := makeBytes(m.str); | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		rune, size := DecodeRune(b); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		if rune != m.rune || size != len(b) { | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 			t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b, rune, size, m.rune, len(b)); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-05-11 14:10:34 -07:00
										 |  |  | 		s := m.str; | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		rune, size = DecodeRuneInString(s); | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 		if rune != m.rune || size != len(b) { | 
					
						
							| 
									
										
										
										
											2009-05-11 14:10:34 -07:00
										 |  |  | 			t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", s, rune, size, m.rune, len(b)); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-16 12:47:24 -08:00
										 |  |  | 		// there's an extra byte that bytes left behind - make sure trailing byte works | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		rune, size = DecodeRune(b[0:cap(b)]); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		if rune != m.rune || size != len(b) { | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 			t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b, rune, size, m.rune, len(b)); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | 		s = m.str + "\x00"; | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		rune, size = DecodeRuneInString(s); | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 		if rune != m.rune || size != len(b) { | 
					
						
							| 
									
										
										
										
											2009-05-11 14:10:34 -07:00
										 |  |  | 			t.Errorf("DecodeRuneInString(%q) = 0x%04x, %d want 0x%04x, %d", s, rune, size, m.rune, len(b)); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// make sure missing bytes fail | 
					
						
							|  |  |  | 		wantsize := 1; | 
					
						
							|  |  |  | 		if wantsize >= len(b) { | 
					
						
							|  |  |  | 			wantsize = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | 		rune, size = DecodeRune(b[0 : len(b)-1]); | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 		if rune != RuneError || size != wantsize { | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | 			t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b[0 : len(b)-1], rune, size, RuneError, wantsize); | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-10-08 15:14:54 -07:00
										 |  |  | 		s = m.str[0 : len(m.str)-1]; | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		rune, size = DecodeRuneInString(s); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		if rune != RuneError || size != wantsize { | 
					
						
							| 
									
										
										
										
											2009-05-11 14:10:34 -07:00
										 |  |  | 			t.Errorf("DecodeRuneInString(%q) = 0x%04x, %d want 0x%04x, %d", s, rune, size, RuneError, wantsize); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// make sure bad sequences fail | 
					
						
							|  |  |  | 		if len(b) == 1 { | 
					
						
							|  |  |  | 			b[0] = 0x80; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			b[len(b)-1] = 0x7F; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		rune, size = DecodeRune(b); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		if rune != RuneError || size != 1 { | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 			t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b, rune, size, RuneError, 1); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-05-11 14:10:34 -07:00
										 |  |  | 		s = string(b); | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		rune, size = DecodeRune(b); | 
					
						
							| 
									
										
										
										
											2008-11-24 14:51:33 -08:00
										 |  |  | 		if rune != RuneError || size != 1 { | 
					
						
							| 
									
										
										
										
											2009-05-11 14:10:34 -07:00
										 |  |  | 			t.Errorf("DecodeRuneInString(%q) = 0x%04x, %d want 0x%04x, %d", s, rune, size, RuneError, 1); | 
					
						
							| 
									
										
										
										
											2008-11-21 16:13:31 -08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-04 21:00:34 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-20 14:40:40 -08:00
										 |  |  | type RuneCountTest struct { | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | 	in	string; | 
					
						
							|  |  |  | 	out	int; | 
					
						
							| 
									
										
										
										
											2008-12-04 21:00:34 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | var runecounttests = []RuneCountTest{ | 
					
						
							|  |  |  | 	RuneCountTest{"abcd", 4}, | 
					
						
							|  |  |  | 	RuneCountTest{"☺☻☹", 3}, | 
					
						
							|  |  |  | 	RuneCountTest{"1,2,3,4", 7}, | 
					
						
							|  |  |  | 	RuneCountTest{"\xe2\x00", 2}, | 
					
						
							| 
									
										
										
										
											2009-03-03 08:39:12 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-10-07 11:55:06 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-20 14:40:40 -08:00
										 |  |  | func TestRuneCount(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2008-12-04 21:00:34 -08:00
										 |  |  | 	for i := 0; i < len(runecounttests); i++ { | 
					
						
							|  |  |  | 		tt := runecounttests[i]; | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		if out := RuneCountInString(tt.in); out != tt.out { | 
					
						
							| 
									
										
										
										
											2008-12-04 21:00:34 -08:00
										 |  |  | 			t.Errorf("RuneCountInString(%q) = %d, want %d", tt.in, out, tt.out); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-08-12 13:18:37 -07:00
										 |  |  | 		if out := RuneCount(makeBytes(tt.in)); out != tt.out { | 
					
						
							| 
									
										
										
										
											2008-12-04 21:00:34 -08:00
										 |  |  | 			t.Errorf("RuneCount(%q) = %d, want %d", tt.in, out, tt.out); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |