]> Cypherpunks repositories - gostls13.git/commitdiff
cgo: fix GoBytes
authorGustavo Niemeyer <gustavo@niemeyer.net>
Tue, 30 Aug 2011 17:33:16 +0000 (14:33 -0300)
committerGustavo Niemeyer <gustavo@niemeyer.net>
Tue, 30 Aug 2011 17:33:16 +0000 (14:33 -0300)
R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/4956051

misc/cgo/test/Makefile
misc/cgo/test/callback.go
misc/cgo/test/cgo_test.go
misc/cgo/test/helpers.go [new file with mode: 0644]
src/pkg/runtime/string.goc

index d4309be3c62245b493c8ce80e434c353b897f10b..5617e78c3726f23007a89b152ccbd5f715b50ebf 100644 (file)
@@ -12,6 +12,7 @@ CGOFILES=\
        callback.go\
        env.go\
        exports.go\
+       helpers.go\
        issue1222.go\
        issue1328.go\
        issue1560.go\
index 3edee975810e504c8423bc98c176c9a793b2b7da..d20790e875f37753dca7d3dedde8ca0d7ebed4ca 100644 (file)
@@ -65,7 +65,7 @@ func testCallbackPanic(t *testing.T) {
 func testCallbackPanicLoop(t *testing.T) {
        // Make sure we don't blow out m->g0 stack.
        for i := 0; i < 100000; i++ {
-               TestCallbackPanic(t)
+               testCallbackPanic(t)
        }
 }
 
index 03f02370a1f4b14eeb3533ed011d819b4efaf19f..34beee69d194d4d387ca738acd6cf041c852fa03 100644 (file)
@@ -26,5 +26,6 @@ func TestBlocking(t *testing.T)            { testBlocking(t) }
 func Test1328(t *testing.T)                { test1328(t) }
 func TestParallelSleep(t *testing.T)       { testParallelSleep(t) }
 func TestSetEnv(t *testing.T)              { testSetEnv(t) }
+func TestHelpers(t *testing.T)             { testHelpers(t) }
 
 func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
diff --git a/misc/cgo/test/helpers.go b/misc/cgo/test/helpers.go
new file mode 100644 (file)
index 0000000..3a4f014
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright 2011 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cgotest
+
+// const char *greeting = "hello, world";
+import "C"
+
+import (
+       "reflect"
+       "testing"
+       "unsafe"
+)
+
+const greeting = "hello, world"
+
+type testPair struct {
+       Name string
+       Got, Want interface{}
+}
+
+var testPairs = []testPair{
+       {"GoString", C.GoString(C.greeting), greeting},
+       {"GoStringN", C.GoStringN(C.greeting, 5), greeting[:5]},
+       {"GoBytes", C.GoBytes(unsafe.Pointer(C.greeting), 5), []byte(greeting[:5])},
+}
+
+func testHelpers(t *testing.T) {
+       for _, pair := range testPairs {
+               if !reflect.DeepEqual(pair.Got, pair.Want) {
+                       t.Errorf("%s: got %#v, want %#v", pair.Got, pair.Want)
+               }
+       }
+}
index 48bf3183b525768579d02b031e556cd7e19c88ad..322706c0c42e783105389a1816fde439a55a7e4f 100644 (file)
@@ -80,6 +80,8 @@ runtime·gobytes(byte *p, int32 n)
        Slice sl;
 
        sl.array = runtime·mallocgc(n, FlagNoPointers, 1, 0);
+       sl.len = n;
+       sl.cap = n;
        runtime·memmove(sl.array, p, n);
        return sl;
 }