dumpcgo.go 3.2 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 code should really only be in the dumpcgo_test.go file,
  17. // but unfortunately Go will not allow cgo in test files, so this is a
  18. // workaround to allow cgo types to be tested. This configuration is used
  19. // because spew itself does not require cgo to run even though it does handle
  20. // certain cgo types specially. Rather than forcing all clients to require cgo
  21. // and an external C compiler just to run the tests, this scheme makes them
  22. // optional.
  23. // +build cgo,testcgo
  24. package testdata
  25. /*
  26. #include <stdint.h>
  27. typedef unsigned char custom_uchar_t;
  28. char *ncp = 0;
  29. char *cp = "test";
  30. char ca[6] = {'t', 'e', 's', 't', '2', '\0'};
  31. unsigned char uca[6] = {'t', 'e', 's', 't', '3', '\0'};
  32. signed char sca[6] = {'t', 'e', 's', 't', '4', '\0'};
  33. uint8_t ui8ta[6] = {'t', 'e', 's', 't', '5', '\0'};
  34. custom_uchar_t tuca[6] = {'t', 'e', 's', 't', '6', '\0'};
  35. */
  36. import "C"
  37. // GetCgoNullCharPointer returns a null char pointer via cgo. This is only
  38. // used for tests.
  39. func GetCgoNullCharPointer() interface{} {
  40. return C.ncp
  41. }
  42. // GetCgoCharPointer returns a char pointer via cgo. This is only used for
  43. // tests.
  44. func GetCgoCharPointer() interface{} {
  45. return C.cp
  46. }
  47. // GetCgoCharArray returns a char array via cgo and the array's len and cap.
  48. // This is only used for tests.
  49. func GetCgoCharArray() (interface{}, int, int) {
  50. return C.ca, len(C.ca), cap(C.ca)
  51. }
  52. // GetCgoUnsignedCharArray returns an unsigned char array via cgo and the
  53. // array's len and cap. This is only used for tests.
  54. func GetCgoUnsignedCharArray() (interface{}, int, int) {
  55. return C.uca, len(C.uca), cap(C.uca)
  56. }
  57. // GetCgoSignedCharArray returns a signed char array via cgo and the array's len
  58. // and cap. This is only used for tests.
  59. func GetCgoSignedCharArray() (interface{}, int, int) {
  60. return C.sca, len(C.sca), cap(C.sca)
  61. }
  62. // GetCgoUint8tArray returns a uint8_t array via cgo and the array's len and
  63. // cap. This is only used for tests.
  64. func GetCgoUint8tArray() (interface{}, int, int) {
  65. return C.ui8ta, len(C.ui8ta), cap(C.ui8ta)
  66. }
  67. // GetCgoTypdefedUnsignedCharArray returns a typedefed unsigned char array via
  68. // cgo and the array's len and cap. This is only used for tests.
  69. func GetCgoTypdefedUnsignedCharArray() (interface{}, int, int) {
  70. return C.tuca, len(C.tuca), cap(C.tuca)
  71. }