github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/test/go-spew/spew/dumpcgo_test.go (about)

     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  
    15  // NOTE: Due to the following build constraints, this file will only be compiled
    16  // when both cgo is supported and "-tags testcgo" is added to the go test
    17  // command line.  This means the cgo tests are only added (and hence run) when
    18  // specifially requested.  This configuration is used because spew itself
    19  // does not require cgo to run even though it does handle certain cgo types
    20  // specially.  Rather than forcing all clients to require cgo and an external
    21  // C compiler just to run the tests, this scheme makes them optional.
    22  // +build cgo,testcgo
    23  
    24  package spew_test
    25  
    26  import (
    27  	"fmt"
    28  
    29  	"github.com/davecgh/go-spew/spew/testdata"
    30  )
    31  
    32  func addCgoDumpTests() {
    33  	// C char pointer.
    34  	v := testdata.GetCgoCharPointer()
    35  	nv := testdata.GetCgoNullCharPointer()
    36  	pv := &v
    37  	vcAddr := fmt.Sprintf("%p", v)
    38  	vAddr := fmt.Sprintf("%p", pv)
    39  	pvAddr := fmt.Sprintf("%p", &pv)
    40  	vt := "*testdata._Ctype_char"
    41  	vs := "116"
    42  	addDumpTest(v, "("+vt+")("+vcAddr+")("+vs+")\n")
    43  	addDumpTest(pv, "(*"+vt+")("+vAddr+"->"+vcAddr+")("+vs+")\n")
    44  	addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+"->"+vcAddr+")("+vs+")\n")
    45  	addDumpTest(nv, "("+vt+")(<nil>)\n")
    46  
    47  	// C char array.
    48  	v2, v2l, v2c := testdata.GetCgoCharArray()
    49  	v2Len := fmt.Sprintf("%d", v2l)
    50  	v2Cap := fmt.Sprintf("%d", v2c)
    51  	v2t := "[6]testdata._Ctype_char"
    52  	v2s := "(len=" + v2Len + " cap=" + v2Cap + ") " +
    53  		"{\n 00000000  74 65 73 74 32 00                               " +
    54  		"  |test2.|\n}"
    55  	addDumpTest(v2, "("+v2t+") "+v2s+"\n")
    56  
    57  	// C unsigned char array.
    58  	v3, v3l, v3c := testdata.GetCgoUnsignedCharArray()
    59  	v3Len := fmt.Sprintf("%d", v3l)
    60  	v3Cap := fmt.Sprintf("%d", v3c)
    61  	v3t := "[6]testdata._Ctype_unsignedchar"
    62  	v3t2 := "[6]testdata._Ctype_uchar"
    63  	v3s := "(len=" + v3Len + " cap=" + v3Cap + ") " +
    64  		"{\n 00000000  74 65 73 74 33 00                               " +
    65  		"  |test3.|\n}"
    66  	addDumpTest(v3, "("+v3t+") "+v3s+"\n", "("+v3t2+") "+v3s+"\n")
    67  
    68  	// C signed char array.
    69  	v4, v4l, v4c := testdata.GetCgoSignedCharArray()
    70  	v4Len := fmt.Sprintf("%d", v4l)
    71  	v4Cap := fmt.Sprintf("%d", v4c)
    72  	v4t := "[6]testdata._Ctype_schar"
    73  	v4t2 := "testdata._Ctype_schar"
    74  	v4s := "(len=" + v4Len + " cap=" + v4Cap + ") " +
    75  		"{\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 101,\n (" + v4t2 +
    76  		") 115,\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 52,\n (" + v4t2 +
    77  		") 0\n}"
    78  	addDumpTest(v4, "("+v4t+") "+v4s+"\n")
    79  
    80  	// C uint8_t array.
    81  	v5, v5l, v5c := testdata.GetCgoUint8tArray()
    82  	v5Len := fmt.Sprintf("%d", v5l)
    83  	v5Cap := fmt.Sprintf("%d", v5c)
    84  	v5t := "[6]testdata._Ctype_uint8_t"
    85  	v5s := "(len=" + v5Len + " cap=" + v5Cap + ") " +
    86  		"{\n 00000000  74 65 73 74 35 00                               " +
    87  		"  |test5.|\n}"
    88  	addDumpTest(v5, "("+v5t+") "+v5s+"\n")
    89  
    90  	// C typedefed unsigned char array.
    91  	v6, v6l, v6c := testdata.GetCgoTypdefedUnsignedCharArray()
    92  	v6Len := fmt.Sprintf("%d", v6l)
    93  	v6Cap := fmt.Sprintf("%d", v6c)
    94  	v6t := "[6]testdata._Ctype_custom_uchar_t"
    95  	v6s := "(len=" + v6Len + " cap=" + v6Cap + ") " +
    96  		"{\n 00000000  74 65 73 74 36 00                               " +
    97  		"  |test6.|\n}"
    98  	addDumpTest(v6, "("+v6t+") "+v6s+"\n")
    99  }