dumpcgo_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2013 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 := testdata.GetCgoCharArray()
  43. v2t := "[6]testdata._Ctype_char"
  44. v2s := "{\n 00000000 74 65 73 74 32 00 " +
  45. " |test2.|\n}"
  46. addDumpTest(v2, "("+v2t+") "+v2s+"\n")
  47. // C unsigned char array.
  48. v3 := testdata.GetCgoUnsignedCharArray()
  49. v3t := "[6]testdata._Ctype_unsignedchar"
  50. v3s := "{\n 00000000 74 65 73 74 33 00 " +
  51. " |test3.|\n}"
  52. addDumpTest(v3, "("+v3t+") "+v3s+"\n")
  53. // C signed char array.
  54. v4 := testdata.GetCgoSignedCharArray()
  55. v4t := "[6]testdata._Ctype_schar"
  56. v4t2 := "testdata._Ctype_schar"
  57. v4s := "{\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 101,\n (" + v4t2 +
  58. ") 115,\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 52,\n (" + v4t2 +
  59. ") 0\n}"
  60. addDumpTest(v4, "("+v4t+") "+v4s+"\n")
  61. // C uint8_t array.
  62. v5 := testdata.GetCgoUint8tArray()
  63. v5t := "[6]testdata._Ctype_uint8_t"
  64. v5s := "{\n 00000000 74 65 73 74 35 00 " +
  65. " |test5.|\n}"
  66. addDumpTest(v5, "("+v5t+") "+v5s+"\n")
  67. // C typedefed unsigned char array.
  68. v6 := testdata.GetCgoTypdefedUnsignedCharArray()
  69. v6t := "[6]testdata._Ctype_custom_uchar_t"
  70. v6s := "{\n 00000000 74 65 73 74 36 00 " +
  71. " |test6.|\n}"
  72. addDumpTest(v6, "("+v6t+") "+v6s+"\n")
  73. }