summaryrefslogtreecommitdiffstats
path: root/src/cmd/cgo/internal/swig/testdata
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:19:13 +0000
commitccd992355df7192993c666236047820244914598 (patch)
treef00fea65147227b7743083c6148396f74cd66935 /src/cmd/cgo/internal/swig/testdata
parentInitial commit. (diff)
downloadgolang-1.21-ccd992355df7192993c666236047820244914598.tar.xz
golang-1.21-ccd992355df7192993c666236047820244914598.zip
Adding upstream version 1.21.8.upstream/1.21.8
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/cmd/cgo/internal/swig/testdata')
-rw-r--r--src/cmd/cgo/internal/swig/testdata/callback/main.cc15
-rw-r--r--src/cmd/cgo/internal/swig/testdata/callback/main.go60
-rw-r--r--src/cmd/cgo/internal/swig/testdata/callback/main.h20
-rw-r--r--src/cmd/cgo/internal/swig/testdata/callback/main.swigcxx18
-rw-r--r--src/cmd/cgo/internal/swig/testdata/stdio/main.go45
-rw-r--r--src/cmd/cgo/internal/swig/testdata/stdio/main.swig24
6 files changed, 182 insertions, 0 deletions
diff --git a/src/cmd/cgo/internal/swig/testdata/callback/main.cc b/src/cmd/cgo/internal/swig/testdata/callback/main.cc
new file mode 100644
index 0000000..7de917c
--- /dev/null
+++ b/src/cmd/cgo/internal/swig/testdata/callback/main.cc
@@ -0,0 +1,15 @@
+// Copyright 2013 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.
+
+// This .cc file will be automatically compiled by the go tool and
+// included in the package.
+
+#include <string>
+#include "main.h"
+
+std::string Caller::call() {
+ if (callback_ != 0)
+ return callback_->run();
+ return "";
+}
diff --git a/src/cmd/cgo/internal/swig/testdata/callback/main.go b/src/cmd/cgo/internal/swig/testdata/callback/main.go
new file mode 100644
index 0000000..73034a0
--- /dev/null
+++ b/src/cmd/cgo/internal/swig/testdata/callback/main.go
@@ -0,0 +1,60 @@
+// Copyright 2012 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 main
+
+import (
+ "fmt"
+ "os"
+)
+
+func main() {
+ if len(os.Args) != 2 {
+ fatal("usage: callback testname")
+ }
+ switch os.Args[1] {
+ default:
+ fatal("unknown test %q", os.Args[1])
+ case "Call":
+ testCall()
+ case "Callback":
+ testCallback()
+ }
+ println("OK")
+}
+
+func fatal(f string, args ...any) {
+ fmt.Fprintln(os.Stderr, fmt.Sprintf(f, args...))
+ os.Exit(1)
+}
+
+type GoCallback struct{}
+
+func (p *GoCallback) Run() string {
+ return "GoCallback.Run"
+}
+
+func testCall() {
+ c := NewCaller()
+ cb := NewCallback()
+
+ c.SetCallback(cb)
+ s := c.Call()
+ if s != "Callback::run" {
+ fatal("unexpected string from Call: %q", s)
+ }
+ c.DelCallback()
+}
+
+func testCallback() {
+ c := NewCaller()
+ cb := NewDirectorCallback(&GoCallback{})
+ c.SetCallback(cb)
+ s := c.Call()
+ if s != "GoCallback.Run" {
+ fatal("unexpected string from Call with callback: %q", s)
+ }
+ c.DelCallback()
+ DeleteDirectorCallback(cb)
+}
diff --git a/src/cmd/cgo/internal/swig/testdata/callback/main.h b/src/cmd/cgo/internal/swig/testdata/callback/main.h
new file mode 100644
index 0000000..4b66106
--- /dev/null
+++ b/src/cmd/cgo/internal/swig/testdata/callback/main.h
@@ -0,0 +1,20 @@
+// 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.
+
+class Callback {
+public:
+ virtual ~Callback() { }
+ virtual std::string run() { return "Callback::run"; }
+};
+
+class Caller {
+private:
+ Callback *callback_;
+public:
+ Caller(): callback_(0) { }
+ ~Caller() { delCallback(); }
+ void delCallback() { delete callback_; callback_ = 0; }
+ void setCallback(Callback *cb) { delCallback(); callback_ = cb; }
+ std::string call();
+};
diff --git a/src/cmd/cgo/internal/swig/testdata/callback/main.swigcxx b/src/cmd/cgo/internal/swig/testdata/callback/main.swigcxx
new file mode 100644
index 0000000..0fd73d6
--- /dev/null
+++ b/src/cmd/cgo/internal/swig/testdata/callback/main.swigcxx
@@ -0,0 +1,18 @@
+/* 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. */
+
+/* An example of writing a C++ virtual function in Go. */
+
+%module(directors="1") callback
+
+%{
+#include <string>
+#include "main.h"
+%}
+
+%include "std_string.i"
+
+%feature("director");
+
+%include "main.h"
diff --git a/src/cmd/cgo/internal/swig/testdata/stdio/main.go b/src/cmd/cgo/internal/swig/testdata/stdio/main.go
new file mode 100644
index 0000000..0296dd3
--- /dev/null
+++ b/src/cmd/cgo/internal/swig/testdata/stdio/main.go
@@ -0,0 +1,45 @@
+// Copyright 2017 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.
+
+// This file is here just to cause problems.
+// main.swig turns into a file also named main.go.
+// Make sure cmd/go keeps them separate
+// when both are passed to cgo.
+
+package main
+
+//int F(void) { return 1; }
+import "C"
+import (
+ "fmt"
+ "os"
+)
+
+func F() int { return int(C.F()) }
+
+func main() {
+ if x := int(C.F()); x != 1 {
+ fatal("x = %d, want 1", x)
+ }
+
+ // Open this file itself and verify that the first few characters are
+ // as expected.
+ f := Fopen("main.go", "r")
+ if f.Swigcptr() == 0 {
+ fatal("fopen failed")
+ }
+ if Fgetc(f) != '/' || Fgetc(f) != '/' || Fgetc(f) != ' ' || Fgetc(f) != 'C' {
+ fatal("read unexpected characters")
+ }
+ if Fclose(f) != 0 {
+ fatal("fclose failed")
+ }
+
+ println("OK")
+}
+
+func fatal(f string, args ...any) {
+ fmt.Fprintln(os.Stderr, fmt.Sprintf(f, args...))
+ os.Exit(1)
+}
diff --git a/src/cmd/cgo/internal/swig/testdata/stdio/main.swig b/src/cmd/cgo/internal/swig/testdata/stdio/main.swig
new file mode 100644
index 0000000..b28ae0a
--- /dev/null
+++ b/src/cmd/cgo/internal/swig/testdata/stdio/main.swig
@@ -0,0 +1,24 @@
+/* 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. */
+
+/* A trivial example of wrapping a C library using SWIG. */
+
+%{
+#include <stdio.h>
+#include <stdlib.h>
+%}
+
+%typemap(gotype) const char * "string"
+%typemap(in) const char * %{
+ $1 = malloc($input.n + 1);
+ memcpy($1, $input.p, $input.n);
+ $1[$input.n] = '\0';
+%}
+%typemap(freearg) const char * %{
+ free($1);
+%}
+
+FILE *fopen(const char *name, const char *mode);
+int fclose(FILE *);
+int fgetc(FILE *);