main_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // 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. package main
  15. import (
  16. "bytes"
  17. "testing"
  18. )
  19. var bytesToAsmTestCases = []struct {
  20. name string
  21. in []byte
  22. out string
  23. }{
  24. {
  25. name: "empty",
  26. in: []byte{},
  27. out: "\n",
  28. },
  29. {
  30. name: "short",
  31. in: []byte{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01},
  32. out: ".byte 127,69,76,70,2,1\n",
  33. },
  34. {
  35. name: "multiline",
  36. in: []byte{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x30, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
  39. 0x50, 0x98, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x88, 0xd1, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00,
  43. 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00,
  44. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00},
  45. 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" +
  46. ".byte 2,0,0,0,64,0,56,0\n",
  47. },
  48. }
  49. func TestBytesToAsm(t *testing.T) {
  50. for _, testcase := range bytesToAsmTestCases {
  51. t.Run(testcase.name, func(t *testing.T) {
  52. buf := bytes.Buffer{}
  53. bytesToAsm(&buf, testcase.in)
  54. if buf.String() != testcase.out {
  55. t.Errorf("input: %#v\n want: %q\n got: %q\n", testcase.in, testcase.out, buf.String())
  56. }
  57. })
  58. }
  59. }