dumpcgo_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
  2. //
  3. // Permission to use, copy, modify, and distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. // NOTE: Due to the following build constraints, this file will only be compiled
  15. // when both cgo is supported and "-tags testcgo" is added to the go test
  16. // command line. This means the cgo tests are only added (and hence run) when
  17. // specifially requested. This configuration is used because spew itself
  18. // does not require cgo to run even though it does handle certain cgo types
  19. // specially. Rather than forcing all clients to require cgo and an external
  20. // C compiler just to run the tests, this scheme makes them optional.
  21. // +build cgo,testcgo
  22. package spew_test
  23. import (
  24. "fmt"
  25. "github.com/davecgh/go-spew/spew/testdata"
  26. )
  27. func addCgoDumpTests() {
  28. // C char pointer.
  29. v := testdata.GetCgoCharPointer()
  30. nv := testdata.GetCgoNullCharPointer()
  31. pv := &v
  32. vcAddr := fmt.Sprintf("%p", v)
  33. vAddr := fmt.Sprintf("%p", pv)
  34. pvAddr := fmt.Sprintf("%p", &pv)
  35. vt := "*testdata._Ctype_char"
  36. vs := "116"
  37. addDumpTest(v, "("+vt+")("+vcAddr+")("+vs+")\n")
  38. addDumpTest(pv, "(*"+vt+")("+vAddr+"->"+vcAddr+")("+vs+")\n")
  39. addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+"->"+vcAddr+")("+vs+")\n")
  40. addDumpTest(nv, "("+vt+")(<nil>)\n")
  41. // C char array.
  42. v2, v2l, v2c := testdata.GetCgoCharArray()
  43. v2Len := fmt.Sprintf("%d", v2l)
  44. v2Cap := fmt.Sprintf("%d", v2c)
  45. v2t := "[6]testdata._Ctype_char"
  46. v2s := "(len=" + v2Len + " cap=" + v2Cap + ") " +
  47. "{\n 00000000 74 65 73 74 32 00 " +
  48. " |test2.|\n}"
  49. addDumpTest(v2, "("+v2t+") "+v2s+"\n")
  50. // C unsigned char array.
  51. v3, v3l, v3c := testdata.GetCgoUnsignedCharArray()
  52. v3Len := fmt.Sprintf("%d", v3l)
  53. v3Cap := fmt.Sprintf("%d", v3c)
  54. v3t := "[6]testdata._Ctype_unsignedchar"
  55. v3t2 := "[6]testdata._Ctype_uchar"
  56. v3s := "(len=" + v3Len + " cap=" + v3Cap + ") " +
  57. "{\n 00000000 74 65 73 74 33 00 " +
  58. " |test3.|\n}"
  59. addDumpTest(v3, "("+v3t+") "+v3s+"\n", "("+v3t2+") "+v3s+"\n")
  60. // C signed char array.
  61. v4, v4l, v4c := testdata.GetCgoSignedCharArray()
  62. v4Len := fmt.Sprintf("%d", v4l)
  63. v4Cap := fmt.Sprintf("%d", v4c)
  64. v4t := "[6]testdata._Ctype_schar"
  65. v4t2 := "testdata._Ctype_schar"
  66. v4s := "(len=" + v4Len + " cap=" + v4Cap + ") " +
  67. "{\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 101,\n (" + v4t2 +
  68. ") 115,\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 52,\n (" + v4t2 +
  69. ") 0\n}"
  70. addDumpTest(v4, "("+v4t+") "+v4s+"\n")
  71. // C uint8_t array.
  72. v5, v5l, v5c := testdata.GetCgoUint8tArray()
  73. v5Len := fmt.Sprintf("%d", v5l)
  74. v5Cap := fmt.Sprintf("%d", v5c)
  75. v5t := "[6]testdata._Ctype_uint8_t"
  76. v5t2 := "[6]testdata._Ctype_uchar"
  77. v5s := "(len=" + v5Len + " cap=" + v5Cap + ") " +
  78. "{\n 00000000 74 65 73 74 35 00 " +
  79. " |test5.|\n}"
  80. addDumpTest(v5, "("+v5t+") "+v5s+"\n", "("+v5t2+") "+v5s+"\n")
  81. // C typedefed unsigned char array.
  82. v6, v6l, v6c := testdata.GetCgoTypdefedUnsignedCharArray()
  83. v6Len := fmt.Sprintf("%d", v6l)
  84. v6Cap := fmt.Sprintf("%d", v6c)
  85. v6t := "[6]testdata._Ctype_custom_uchar_t"
  86. v6t2 := "[6]testdata._Ctype_uchar"
  87. v6s := "(len=" + v6Len + " cap=" + v6Cap + ") " +
  88. "{\n 00000000 74 65 73 74 36 00 " +
  89. " |test6.|\n}"
  90. addDumpTest(v6, "("+v6t+") "+v6s+"\n", "("+v6t2+") "+v6s+"\n")
  91. }