| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2010 Google Inc. All rights reserved. |
| 4 | // http://code.google.com/p/goprotobuf/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | package proto_test |
| 33 | |
| 34 | import ( |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 35 | "reflect" |
| 36 | "testing" |
| David Symonds | 20c7366 | 2012-01-20 07:32:21 +1100 | [diff] [blame] | 37 | |
| David Symonds | 704096f | 2012-03-15 15:10:26 +1100 | [diff] [blame] | 38 | . "./testdata" |
| David Symonds | 20c7366 | 2012-01-20 07:32:21 +1100 | [diff] [blame] | 39 | . "code.google.com/p/goprotobuf/proto" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | type UnmarshalTextTest struct { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 43 | in string |
| 44 | err string // if "", no error expected |
| 45 | out *MyMessage |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 48 | func buildExtStructTest(text string) UnmarshalTextTest { |
| 49 | msg := &MyMessage{ |
| 50 | Count: Int32(42), |
| 51 | } |
| 52 | SetExtension(msg, E_Ext_More, &Ext{ |
| 53 | Data: String("Hello, world!"), |
| 54 | }) |
| 55 | return UnmarshalTextTest{in: text, out: msg} |
| 56 | } |
| 57 | |
| 58 | func buildExtDataTest(text string) UnmarshalTextTest { |
| 59 | msg := &MyMessage{ |
| 60 | Count: Int32(42), |
| 61 | } |
| 62 | SetExtension(msg, E_Ext_Text, String("Hello, world!")) |
| 63 | SetExtension(msg, E_Ext_Number, Int32(1729)) |
| 64 | return UnmarshalTextTest{in: text, out: msg} |
| 65 | } |
| 66 | |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame^] | 67 | func buildExtRepStringTest(text string) UnmarshalTextTest { |
| 68 | msg := &MyMessage{ |
| 69 | Count: Int32(42), |
| 70 | } |
| 71 | if err := SetExtension(msg, E_Greeting, []string{"bula", "hola"}); err != nil { |
| 72 | panic(err) |
| 73 | } |
| 74 | return UnmarshalTextTest{in: text, out: msg} |
| 75 | } |
| 76 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 77 | var unMarshalTextTests = []UnmarshalTextTest{ |
| 78 | // Basic |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 79 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 80 | in: " count:42\n name:\"Dave\" ", |
| 81 | out: &MyMessage{ |
| 82 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 83 | Name: String("Dave"), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 84 | }, |
| 85 | }, |
| 86 | |
| 87 | // Empty quoted string |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 88 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 89 | in: `count:42 name:""`, |
| 90 | out: &MyMessage{ |
| 91 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 92 | Name: String(""), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 93 | }, |
| 94 | }, |
| 95 | |
| 96 | // Quoted string concatenation |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 97 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 98 | in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`, |
| 99 | out: &MyMessage{ |
| 100 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 101 | Name: String("My name is elsewhere"), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 102 | }, |
| 103 | }, |
| 104 | |
| David Symonds | 183124e | 2012-03-23 13:20:23 +1100 | [diff] [blame] | 105 | // Quoted string with escaped apostrophe |
| 106 | { |
| 107 | in: `count:42 name: "HOLIDAY - New Year\'s Day"`, |
| 108 | out: &MyMessage{ |
| 109 | Count: Int32(42), |
| 110 | Name: String("HOLIDAY - New Year's Day"), |
| 111 | }, |
| 112 | }, |
| 113 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 114 | // Bad quoted string |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 115 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 116 | in: `inner: < host: "\0" >` + "\n", |
| 117 | err: `line 1.15: invalid quoted string "\0"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 118 | }, |
| 119 | |
| 120 | // Number too large for int64 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 121 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 122 | in: "count: 123456789012345678901", |
| 123 | err: "line 1.7: invalid int32: 123456789012345678901", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 124 | }, |
| 125 | |
| 126 | // Number too large for int32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 127 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 128 | in: "count: 1234567890123", |
| 129 | err: "line 1.7: invalid int32: 1234567890123", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 130 | }, |
| 131 | |
| 132 | // Number too large for float32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 133 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 134 | in: "others:< weight: 12345678901234567890123456789012345678901234567890 >", |
| 135 | err: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 136 | }, |
| 137 | |
| 138 | // Number posing as a quoted string |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 139 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 140 | in: `inner: < host: 12 >` + "\n", |
| 141 | err: `line 1.15: invalid string: 12`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 142 | }, |
| 143 | |
| 144 | // Quoted string posing as int32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 145 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 146 | in: `count: "12"`, |
| 147 | err: `line 1.7: invalid int32: "12"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 148 | }, |
| 149 | |
| 150 | // Quoted string posing a float32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 151 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 152 | in: `others:< weight: "17.4" >`, |
| 153 | err: `line 1.17: invalid float32: "17.4"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 154 | }, |
| 155 | |
| 156 | // Enum |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 157 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 158 | in: `count:42 bikeshed: BLUE`, |
| 159 | out: &MyMessage{ |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 160 | Count: Int32(42), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 161 | Bikeshed: NewMyMessage_Color(MyMessage_BLUE), |
| 162 | }, |
| 163 | }, |
| 164 | |
| 165 | // Repeated field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 166 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 167 | in: `count:42 pet: "horsey" pet:"bunny"`, |
| 168 | out: &MyMessage{ |
| 169 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 170 | Pet: []string{"horsey", "bunny"}, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 171 | }, |
| 172 | }, |
| 173 | |
| Rob Pike | aaf695a | 2010-06-22 15:51:21 -0700 | [diff] [blame] | 174 | // Repeated message with/without colon and <>/{} |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 175 | { |
| Rob Pike | aaf695a | 2010-06-22 15:51:21 -0700 | [diff] [blame] | 176 | in: `count:42 others:{} others{} others:<> others:{}`, |
| 177 | out: &MyMessage{ |
| 178 | Count: Int32(42), |
| 179 | Others: []*OtherMessage{ |
| 180 | &OtherMessage{}, |
| 181 | &OtherMessage{}, |
| 182 | &OtherMessage{}, |
| 183 | &OtherMessage{}, |
| 184 | }, |
| 185 | }, |
| 186 | }, |
| 187 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 188 | // Missing colon for inner message |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 189 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 190 | in: `count:42 inner < host: "cauchy.syd" >`, |
| 191 | out: &MyMessage{ |
| 192 | Count: Int32(42), |
| 193 | Inner: &InnerMessage{ |
| 194 | Host: String("cauchy.syd"), |
| 195 | }, |
| 196 | }, |
| 197 | }, |
| 198 | |
| 199 | // Missing colon for string field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 200 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 201 | in: `name "Dave"`, |
| 202 | err: `line 1.5: expected ':', found "\"Dave\""`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 203 | }, |
| 204 | |
| 205 | // Missing colon for int32 field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 206 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 207 | in: `count 42`, |
| 208 | err: `line 1.6: expected ':', found "42"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 209 | }, |
| 210 | |
| 211 | // Missing required field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 212 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 213 | in: ``, |
| Rob Pike | b7907bf | 2012-02-13 14:25:20 +1100 | [diff] [blame] | 214 | err: `line 1.0: message testdata.MyMessage missing required field "count"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 215 | }, |
| 216 | |
| 217 | // Repeated non-repeated field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 218 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 219 | in: `name: "Rob" name: "Russ"`, |
| 220 | err: `line 1.12: non-repeated field "name" was repeated`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 221 | }, |
| 222 | |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 223 | // Group |
| 224 | { |
| 225 | in: `count: 17 SomeGroup { group_field: 12 }`, |
| 226 | out: &MyMessage{ |
| 227 | Count: Int32(17), |
| 228 | Somegroup: &MyMessage_SomeGroup{ |
| 229 | GroupField: Int32(12), |
| 230 | }, |
| 231 | }, |
| 232 | }, |
| 233 | |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 234 | // Extension |
| Rob Pike | b7907bf | 2012-02-13 14:25:20 +1100 | [diff] [blame] | 235 | buildExtStructTest(`count: 42 [testdata.Ext.more]:<data:"Hello, world!" >`), |
| 236 | buildExtStructTest(`count: 42 [testdata.Ext.more] {data:"Hello, world!"}`), |
| 237 | buildExtDataTest(`count: 42 [testdata.Ext.text]:"Hello, world!" [testdata.Ext.number]:1729`), |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame^] | 238 | buildExtRepStringTest(`count: 42 [testdata.greeting]:"bula" [testdata.greeting]:"hola"`), |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 239 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 240 | // Big all-in-one |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 241 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 242 | in: "count:42 # Meaning\n" + |
| 243 | `name:"Dave" ` + |
| 244 | `quote:"\"I didn't want to go.\"" ` + |
| 245 | `pet:"bunny" ` + |
| 246 | `pet:"kitty" ` + |
| 247 | `pet:"horsey" ` + |
| 248 | `inner:<` + |
| 249 | ` host:"footrest.syd" ` + |
| 250 | ` port:7001 ` + |
| 251 | ` connected:true ` + |
| 252 | `> ` + |
| 253 | `others:<` + |
| 254 | ` key:3735928559 ` + |
| 255 | ` value:"\x01A\a\f" ` + |
| 256 | `> ` + |
| 257 | `others:<` + |
| 258 | " weight:58.9 # Atomic weight of Co\n" + |
| 259 | ` inner:<` + |
| 260 | ` host:"lesha.mtv" ` + |
| 261 | ` port:8002 ` + |
| 262 | ` >` + |
| 263 | `>`, |
| 264 | out: &MyMessage{ |
| 265 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 266 | Name: String("Dave"), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 267 | Quote: String(`"I didn't want to go."`), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 268 | Pet: []string{"bunny", "kitty", "horsey"}, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 269 | Inner: &InnerMessage{ |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 270 | Host: String("footrest.syd"), |
| 271 | Port: Int32(7001), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 272 | Connected: Bool(true), |
| 273 | }, |
| 274 | Others: []*OtherMessage{ |
| 275 | &OtherMessage{ |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 276 | Key: Int64(3735928559), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 277 | Value: []byte{0x1, 'A', '\a', '\f'}, |
| 278 | }, |
| 279 | &OtherMessage{ |
| 280 | Weight: Float32(58.9), |
| 281 | Inner: &InnerMessage{ |
| 282 | Host: String("lesha.mtv"), |
| 283 | Port: Int32(8002), |
| 284 | }, |
| 285 | }, |
| 286 | }, |
| 287 | }, |
| 288 | }, |
| 289 | } |
| 290 | |
| 291 | func TestUnmarshalText(t *testing.T) { |
| 292 | for i, test := range unMarshalTextTests { |
| 293 | pb := new(MyMessage) |
| 294 | err := UnmarshalText(test.in, pb) |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 295 | if test.err == "" { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 296 | // We don't expect failure. |
| 297 | if err != nil { |
| 298 | t.Errorf("Test %d: Unexpected error: %v", i, err) |
| 299 | } else if !reflect.DeepEqual(pb, test.out) { |
| David Symonds | ef8f0e8 | 2011-10-13 12:57:34 +1100 | [diff] [blame] | 300 | t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v", |
| 301 | i, pb, test.out) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 302 | } |
| 303 | } else { |
| 304 | // We do expect failure. |
| 305 | if err == nil { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 306 | t.Errorf("Test %d: Didn't get expected error: %v", i, test.err) |
| 307 | } else if err.Error() != test.err { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 308 | t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v", |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 309 | i, err.Error(), test.err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 314 | |
| David Symonds | ef8f0e8 | 2011-10-13 12:57:34 +1100 | [diff] [blame] | 315 | // Regression test; this caused a panic. |
| 316 | func TestRepeatedEnum(t *testing.T) { |
| 317 | pb := new(RepeatedEnum) |
| 318 | if err := UnmarshalText("color: RED", pb); err != nil { |
| 319 | t.Fatal(err) |
| 320 | } |
| 321 | exp := &RepeatedEnum{ |
| 322 | Color: []RepeatedEnum_Color{RepeatedEnum_RED}, |
| 323 | } |
| 324 | if !reflect.DeepEqual(pb, exp) { |
| 325 | t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp) |
| 326 | } |
| 327 | } |
| 328 | |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 329 | var benchInput string |
| 330 | |
| 331 | func init() { |
| 332 | benchInput = "count: 4\n" |
| 333 | for i := 0; i < 1000; i++ { |
| 334 | benchInput += "pet: \"fido\"\n" |
| 335 | } |
| 336 | |
| 337 | // Check it is valid input. |
| 338 | pb := new(MyMessage) |
| 339 | err := UnmarshalText(benchInput, pb) |
| 340 | if err != nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 341 | panic("Bad benchmark input: " + err.Error()) |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | func BenchmarkUnmarshalText(b *testing.B) { |
| 346 | pb := new(MyMessage) |
| 347 | for i := 0; i < b.N; i++ { |
| 348 | UnmarshalText(benchInput, pb) |
| 349 | } |
| 350 | b.SetBytes(int64(len(benchInput))) |
| 351 | } |