* reload1.c (eliminate_regs_1): Call gen_rtx_raw_SUBREG for SUBREGs
[official-gcc.git] / libgo / go / mime / quotedprintable / writer_test.go
bloba9b77b3f9892ca9a85bd816661e2b1179f2fc882
1 // Copyright 2015 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.
5 package quotedprintable
7 import (
8 "bytes"
9 "io/ioutil"
10 "strings"
11 "testing"
14 func TestWriter(t *testing.T) {
15 testWriter(t, false)
18 func TestWriterBinary(t *testing.T) {
19 testWriter(t, true)
22 func testWriter(t *testing.T, binary bool) {
23 tests := []struct {
24 in, want, wantB string
26 {in: "", want: ""},
27 {in: "foo bar", want: "foo bar"},
28 {in: "foo bar=", want: "foo bar=3D"},
29 {in: "foo bar\r", want: "foo bar\r\n", wantB: "foo bar=0D"},
30 {in: "foo bar\r\r", want: "foo bar\r\n\r\n", wantB: "foo bar=0D=0D"},
31 {in: "foo bar\n", want: "foo bar\r\n", wantB: "foo bar=0A"},
32 {in: "foo bar\r\n", want: "foo bar\r\n", wantB: "foo bar=0D=0A"},
33 {in: "foo bar\r\r\n", want: "foo bar\r\n\r\n", wantB: "foo bar=0D=0D=0A"},
34 {in: "foo bar ", want: "foo bar=20"},
35 {in: "foo bar\t", want: "foo bar=09"},
36 {in: "foo bar ", want: "foo bar =20"},
37 {in: "foo bar \n", want: "foo bar=20\r\n", wantB: "foo bar =0A"},
38 {in: "foo bar \r", want: "foo bar=20\r\n", wantB: "foo bar =0D"},
39 {in: "foo bar \r\n", want: "foo bar=20\r\n", wantB: "foo bar =0D=0A"},
40 {in: "foo bar \n", want: "foo bar =20\r\n", wantB: "foo bar =0A"},
41 {in: "foo bar \n ", want: "foo bar =20\r\n=20", wantB: "foo bar =0A=20"},
42 {in: "¡Hola Señor!", want: "=C2=A1Hola Se=C3=B1or!"},
44 in: "\t !\"#$%&'()*+,-./ :;<>?@[\\]^_`{|}~",
45 want: "\t !\"#$%&'()*+,-./ :;<>?@[\\]^_`{|}~",
48 in: strings.Repeat("a", 75),
49 want: strings.Repeat("a", 75),
52 in: strings.Repeat("a", 76),
53 want: strings.Repeat("a", 75) + "=\r\na",
56 in: strings.Repeat("a", 72) + "=",
57 want: strings.Repeat("a", 72) + "=3D",
60 in: strings.Repeat("a", 73) + "=",
61 want: strings.Repeat("a", 73) + "=\r\n=3D",
64 in: strings.Repeat("a", 74) + "=",
65 want: strings.Repeat("a", 74) + "=\r\n=3D",
68 in: strings.Repeat("a", 75) + "=",
69 want: strings.Repeat("a", 75) + "=\r\n=3D",
72 in: strings.Repeat(" ", 73),
73 want: strings.Repeat(" ", 72) + "=20",
76 in: strings.Repeat(" ", 74),
77 want: strings.Repeat(" ", 73) + "=\r\n=20",
80 in: strings.Repeat(" ", 75),
81 want: strings.Repeat(" ", 74) + "=\r\n=20",
84 in: strings.Repeat(" ", 76),
85 want: strings.Repeat(" ", 75) + "=\r\n=20",
88 in: strings.Repeat(" ", 77),
89 want: strings.Repeat(" ", 75) + "=\r\n =20",
93 for _, tt := range tests {
94 buf := new(bytes.Buffer)
95 w := NewWriter(buf)
97 want := tt.want
98 if binary {
99 w.Binary = true
100 if tt.wantB != "" {
101 want = tt.wantB
105 if _, err := w.Write([]byte(tt.in)); err != nil {
106 t.Errorf("Write(%q): %v", tt.in, err)
107 continue
109 if err := w.Close(); err != nil {
110 t.Errorf("Close(): %v", err)
111 continue
113 got := buf.String()
114 if got != want {
115 t.Errorf("Write(%q), got:\n%q\nwant:\n%q", tt.in, got, want)
120 func TestRoundTrip(t *testing.T) {
121 buf := new(bytes.Buffer)
122 w := NewWriter(buf)
123 if _, err := w.Write(testMsg); err != nil {
124 t.Fatalf("Write: %v", err)
126 if err := w.Close(); err != nil {
127 t.Fatalf("Close: %v", err)
130 r := NewReader(buf)
131 gotBytes, err := ioutil.ReadAll(r)
132 if err != nil {
133 t.Fatalf("Error while reading from Reader: %v", err)
135 got := string(gotBytes)
136 if got != string(testMsg) {
137 t.Errorf("Encoding and decoding changed the message, got:\n%s", got)
141 // From http://fr.wikipedia.org/wiki/Quoted-Printable
142 var testMsg = []byte("Quoted-Printable (QP) est un format d'encodage de données codées sur 8 bits, qui utilise exclusivement les caractères alphanumériques imprimables du code ASCII (7 bits).\r\n" +
143 "\r\n" +
144 "En effet, les différents codages comprennent de nombreux caractères qui ne sont pas représentables en ASCII (par exemple les caractères accentués), ainsi que des caractères dits « non-imprimables ».\r\n" +
145 "\r\n" +
146 "L'encodage Quoted-Printable permet de remédier à ce problème, en procédant de la manière suivante :\r\n" +
147 "\r\n" +
148 "Un octet correspondant à un caractère imprimable de l'ASCII sauf le signe égal (donc un caractère de code ASCII entre 33 et 60 ou entre 62 et 126) ou aux caractères de saut de ligne (codes ASCII 13 et 10) ou une suite de tabulations et espaces non situées en fin de ligne (de codes ASCII respectifs 9 et 32) est représenté tel quel.\r\n" +
149 "Un octet qui ne correspond pas à la définition ci-dessus (caractère non imprimable de l'ASCII, tabulation ou espaces non suivies d'un caractère imprimable avant la fin de la ligne ou signe égal) est représenté par un signe égal, suivi de son numéro, exprimé en hexadécimal.\r\n" +
150 "Enfin, un signe égal suivi par un saut de ligne (donc la suite des trois caractères de codes ASCII 61, 13 et 10) peut être inséré n'importe où, afin de limiter la taille des lignes produites si nécessaire. Une limite de 76 caractères par ligne est généralement respectée.\r\n")
152 func BenchmarkWriter(b *testing.B) {
153 for i := 0; i < b.N; i++ {
154 w := NewWriter(ioutil.Discard)
155 w.Write(testMsg)
156 w.Close()