Skip to content

Instantly share code, notes, and snippets.

@ScottBrooks
Created October 8, 2009 18:52
Show Gist options
  • Save ScottBrooks/205271 to your computer and use it in GitHub Desktop.
Save ScottBrooks/205271 to your computer and use it in GitHub Desktop.
From 5a0d42c687b8d61a526697fe05cb86d88046b529 Mon Sep 17 00:00:00 2001
From: Scott Brooks <scott.brooks@epicadvertising.com>
Date: Thu, 8 Oct 2009 14:50:30 -0400
Subject: [PATCH] failing spec
---
libtest/StructTest.c | 25 +++++++++++++++++++++++++
spec/ffi/struct_spec.rb | 13 +++++++++++++
2 files changed, 38 insertions(+), 0 deletions(-)
diff --git a/libtest/StructTest.c b/libtest/StructTest.c
index ca55638..7b15b7e 100644
--- a/libtest/StructTest.c
+++ b/libtest/StructTest.c
@@ -30,6 +30,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
+#include <stdarg.h>
typedef char s8;
typedef short s16;
@@ -215,3 +216,27 @@ struct_s8s32_s64_ret_s64(struct s8s32 s, long long s64)
{
return s64;
}
+
+// Pass a char *, copy into buffer length struct
+struct struct_string
+{
+ char *bytes;
+ int len;
+};
+
+struct struct_string
+struct_varargs_ret_struct_string(int len, ...)
+{
+ struct struct_string ss;
+ va_list vl;
+
+ va_start(vl, len);
+
+ ss.len = len;
+ ss.bytes = va_arg(vl, char *);
+
+ va_end(vl);
+
+ return ss;
+}
+
diff --git a/spec/ffi/struct_spec.rb b/spec/ffi/struct_spec.rb
index d5f1cd8..6deafad 100644
--- a/spec/ffi/struct_spec.rb
+++ b/spec/ffi/struct_spec.rb
@@ -387,12 +387,18 @@ describe FFI::Struct, ' by value' do
class S8S32 < FFI::Struct
layout :s8, :char, :s32, :int
end
+
+ class StructString < FFI::Struct
+ layout :bytes, :string, :len, :int
+ end
+
attach_function :struct_return_s8s32, [ ], S8S32.by_value
attach_function :struct_s8s32_set, [ :char, :int ], S8S32.by_value
attach_function :struct_s8s32_get_s8, [ S8S32.by_value ], :char
attach_function :struct_s8s32_get_s32, [ S8S32.by_value ], :int
attach_function :struct_s8s32_s32_ret_s32, [ S8S32.by_value, :int ], :int
attach_function :struct_s8s32_s64_ret_s64, [ S8S32.by_value, :long_long ], :long_long
+ attach_function :struct_varargs_ret_struct_string, [ :int, :varargs ], StructString.by_value
end
it 'return using pre-set values' do
@@ -430,6 +436,13 @@ describe FFI::Struct, ' by value' do
LibTest.struct_s8s32_s64_ret_s64(s, 0xdeadcafebabe).should == 0xdeadcafebabe
end
+
+ it 'varargs returning a struct' do
+ string = "test"
+ s = LibTest.struct_varargs_ret_struct_string(4, :string, string)
+ s[:len].should == string.length
+ s[:bytes].should == string
+ end
end
describe FFI::Struct, ' with an array field' do
--
1.6.4.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment