github.com/huandu/go@v0.0.0-20151114150818-04e615e41150/misc/cgo/test/helpers.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cgotest 6 7 // const char *greeting = "hello, world"; 8 import "C" 9 10 import ( 11 "reflect" 12 "testing" 13 "unsafe" 14 ) 15 16 const greeting = "hello, world" 17 18 type testPair struct { 19 Name string 20 Got, Want interface{} 21 } 22 23 var testPairs = []testPair{ 24 {"GoString", C.GoString(C.greeting), greeting}, 25 {"GoStringN", C.GoStringN(C.greeting, 5), greeting[:5]}, 26 {"GoBytes", C.GoBytes(unsafe.Pointer(C.greeting), 5), []byte(greeting[:5])}, 27 } 28 29 func testHelpers(t *testing.T) { 30 for _, pair := range testPairs { 31 if !reflect.DeepEqual(pair.Got, pair.Want) { 32 t.Errorf("%s: got %#v, want %#v", pair.Name, pair.Got, pair.Want) 33 } 34 } 35 }