github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/redis/format_test.go (about) 1 // Copyright 2014 <chaishushan{AT}gmail.com>. 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. 4 5 package redis 6 7 import ( 8 . "chai2010.gopkg/check" 9 ) 10 11 type FormatSuite struct{} 12 13 var _ = Suite(&FormatSuite{}) 14 15 func (s *FormatSuite) TestFormatArg(c *C) { 16 c.Check(formatArg("foo"), DeepEquals, []byte("$3\r\nfoo\r\n")) 17 c.Check(formatArg("世界"), DeepEquals, []byte("$6\r\n\xe4\xb8\x96\xe7\x95\x8c\r\n")) 18 c.Check(formatArg(int(5)), DeepEquals, []byte("$1\r\n5\r\n")) 19 c.Check(formatArg(int8(5)), DeepEquals, []byte("$1\r\n5\r\n")) 20 c.Check(formatArg(int16(5)), DeepEquals, []byte("$1\r\n5\r\n")) 21 c.Check(formatArg(int32(5)), DeepEquals, []byte("$1\r\n5\r\n")) 22 c.Check(formatArg(int64(5)), DeepEquals, []byte("$1\r\n5\r\n")) 23 c.Check(formatArg(uint(5)), DeepEquals, []byte("$1\r\n5\r\n")) 24 c.Check(formatArg(uint8(5)), DeepEquals, []byte("$1\r\n5\r\n")) 25 c.Check(formatArg(uint16(5)), DeepEquals, []byte("$1\r\n5\r\n")) 26 c.Check(formatArg(uint32(5)), DeepEquals, []byte("$1\r\n5\r\n")) 27 c.Check(formatArg(uint64(5)), DeepEquals, []byte("$1\r\n5\r\n")) 28 c.Check(formatArg(true), DeepEquals, []byte("$1\r\n1\r\n")) 29 c.Check(formatArg(false), DeepEquals, []byte("$1\r\n0\r\n")) 30 c.Check(formatArg([]interface{}{"foo", 5, true}), DeepEquals, 31 []byte("$3\r\nfoo\r\n$1\r\n5\r\n$1\r\n1\r\n")) 32 c.Check(formatArg(map[interface{}]interface{}{1: "foo"}), DeepEquals, 33 []byte("$1\r\n1\r\n$3\r\nfoo\r\n")) 34 c.Check(formatArg(1.5), DeepEquals, []byte("$3\r\n1.5\r\n")) 35 } 36 37 func (s *FormatSuite) TestCreateRequest(c *C) { 38 c.Check(createRequest(&request{cmd: "PING"}), DeepEquals, []byte("*1\r\n$4\r\nPING\r\n")) 39 c.Check(createRequest(&request{ 40 cmd: "SET", 41 args: []interface{}{"key", 5}, 42 }), 43 DeepEquals, []byte("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$1\r\n5\r\n")) 44 } 45 46 func (s *FormatSuite) BenchmarkCreateRequest(c *C) { 47 for i := 0; i < c.N; i++ { 48 createRequest(&request{ 49 cmd: "SET", 50 args: []interface{}{"key", "bar"}, 51 }) 52 } 53 } 54 55 func (s *FormatSuite) BenchmarkCreateRequestSlice(c *C) { 56 for i := 0; i < c.N; i++ { 57 createRequest(&request{ 58 cmd: "SET", 59 args: []interface{}{[]string{"key", "bar"}}, 60 }) 61 } 62 }