github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_encoder_native_ptr_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"log"
     5  	"testing"
     6  	"unsafe"
     7  )
     8  
     9  func TestValEncoder_Ptr(t *testing.T) {
    10  	f := func(t *testing.T, o interface{}) {
    11  		checkEncodeValueWithStandard(t, o, nil)
    12  	}
    13  	t.Run("nil", func(t *testing.T) {
    14  		f(t, nil)
    15  	})
    16  	t.Run("true", func(t *testing.T) {
    17  		b := true
    18  		log.Printf("&pb %x", (unsafe.Pointer)(&b))
    19  		//log.Printf("*(&pb) %x", *(*unsafe.Pointer)((unsafe.Pointer)(&b)))
    20  		f(t, &b)
    21  	})
    22  	t.Run("false", func(t *testing.T) {
    23  		b := false
    24  		log.Printf("&pb %p", (unsafe.Pointer)(&b))
    25  		//log.Printf("*(&pb) %x", *(*unsafe.Pointer)((unsafe.Pointer)(&b)))
    26  		f(t, &b)
    27  	})
    28  	t.Run("pptr", func(t *testing.T) {
    29  		f(t, (**bool)(nil))
    30  	})
    31  	t.Run("pptr 2", func(t *testing.T) {
    32  		pb := (*bool)(nil)
    33  		log.Printf("&pb %p", (unsafe.Pointer)(&pb))
    34  		log.Printf("*(&pb) %x", *(*unsafe.Pointer)((unsafe.Pointer)(&pb)))
    35  		f(t, &pb)
    36  	})
    37  	t.Run("pptr 3", func(t *testing.T) {
    38  		b := true
    39  		log.Printf("&b %p", &b)
    40  		pb := &b
    41  		log.Printf("&pb %p", (unsafe.Pointer)(&pb))
    42  		log.Printf("*(&pb) %x", *(*unsafe.Pointer)((unsafe.Pointer)(&pb)))
    43  		f(t, &pb)
    44  	})
    45  }