blob: 6ac4ec60e29a76930d46d667c39930ef8fd4c842 [file] [log] [blame]
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18 "bytes"
19 "testing"
20)
21
22var bytesToAsmTestCases = []struct {
23 name string
24 in []byte
25 out string
26}{
27 {
28 name: "empty",
29 in: []byte{},
30 out: "\n",
31 },
32 {
33 name: "short",
34 in: []byte{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01},
35 out: ".byte 127,69,76,70,2,1\n",
36 },
37 {
38 name: "multiline",
39 in: []byte{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x30, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
42 0x50, 0x98, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x88, 0xd1, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00,
46 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00,
47 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00},
48 out: ".byte 127,69,76,70,2,1,1,0,0,0,0,0,0,0,0,0,48,0,62,0,1,0,0,0,80,152,2,0,0,0,0,0,64,0,0,0,0,0,0,0,136,209,18,0,0,0,0,0,0,0,0,0,64,0,56,0,1,0,0,0,64,0,56,0\n" +
49 ".byte 2,0,0,0,64,0,56,0\n",
50 },
51}
52
53func TestBytesToAsm(t *testing.T) {
54 for _, testcase := range bytesToAsmTestCases {
55 t.Run(testcase.name, func(t *testing.T) {
56 buf := bytes.Buffer{}
57 bytesToAsm(&buf, testcase.in)
58 if buf.String() != testcase.out {
59 t.Errorf("input: %#v\n want: %q\n got: %q\n", testcase.in, testcase.out, buf.String())
60 }
61 })
62 }
63}