Debian Codegraph @

t/ffi_platypus_type_widestring.t

raw
1 use Test2::V0 -no_srand => 1;
2 use open ':std', ':encoding(utf8)';
3 use FFI::CheckLib;
4 use FFI::Platypus;
5 use FFI::Platypus::Memory qw( free strdup );
6 use FFI::Platypus::Type::WideString;
7
8 my($encoding,$width) = eval { FFI::Platypus::Type::WideString->_compute_wide_string_encoding() };
9
10 if(my $error = $@)
11 {
12 $error =~ s/ at .*$//;
13 skip_all "Unable to detect wide string details: $error\n";
14 }
15
16 # This test assumes a wchar_t of at least 2 bytes, which is probably true
17 # everywhere Platypus is actually suppored, but wchar_t could technically
18 # be the same size as char.
19
20 note "encoding = $encoding";
21 note "width = $width";
22
23 my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi'; # need test lib for pointer_is_null
24 push @lib, undef; # for libc wcs* functions
25
26 my $ffi = FFI::Platypus->new( api => 1, lib => \@lib );
27 $ffi->ignore_not_found(1);
28 $ffi->load_custom_type('::WideString' => 'wstring');
29 $ffi->load_custom_type('::WideString' => 'wstring_w', access => 'write');
30
31 my $wcsdup = do {
32
33 our $ptr;
34 my $wrapper = sub {
35 my $xsub = shift;
36 free $ptr if defined $ptr;
37 $ptr = undef;
38 $ptr = $xsub->(@_);
39 };
40
41 my $wcsdup = $ffi->function( wcsdup => ['wstring'] => 'opaque' => $wrapper);
42
43 $wcsdup = $ffi->function( _wcsdup => ['wstring'] => 'opaque' => $wrapper)
44 if $^O eq 'MSWin32' && ! defined $wcsdup;
45
46 END { free $ptr if defined $ptr; undef $ptr };
47
48 $wcsdup;
49 };
50
51 subtest 'wcschr' => sub {
52
53 my $wcschr = $ffi->function( wcschr => ['opaque','wchar_t'] => 'wstring' );
54 skip_all 'Test requires wcschr' unless defined $wcschr;
55 skip_all 'Test requires wcsdup' unless defined $wcsdup;
56
57 is( $ffi->cast( opaque => 'wstring', $wcsdup->call("I \x{2764} Platypus")), "I \x{2764} Platypus" );
58
59 # make sure libc is using the same wchar_t as we are.
60 # also tests "in as argument" mode.
61 is( $wcschr->call($wcsdup->call('foobar'), ord('b')), 'bar');
62 is( $wcschr->call($wcsdup->call("I \x{2764} Platypus"), ord("\x{2764}")), "\x{2764} Platypus");
63
64 };
65
66 my @strings = (
67 [ "trivial" => "" ],
68 [ "simple" => "abcde" ],
69 [ "fancy" => "abcd\x{E9}" ],
70 [ "complex" => "I \x{2764} Platypus" ],
71 );
72
73 subtest 'wide string as argument (in)' => sub {
74
75 my $wcslen = $ffi->function( wcslen => ['wstring'] => 'size_t' );
76 skip_all 'Test requires wcslen' unless defined $wcslen;
77
78 foreach my $test (@strings)
79 {
80 my($name, $string) = @$test;
81 # note: this works because on Windows with UTF_16
82 # because all of our test strings are in the BMP
83 is($wcslen->call($string), length($string), $name);
84 }
85
86 is($ffi->cast( 'wstring', 'opaque', undef), undef, 'NULL');
87
88 };
89
90 subtest 'wide string as argument (out)' => sub {
91
92 my $wcscpy = $ffi->function( wcscpy => ['wstring_w','wstring'] );
93 skip_all 'Test requires wcscpy' unless defined $wcscpy;
94
95 foreach my $test (@strings)
96 {
97 my($name, $string) = @$test;
98
99 my $out1;
100 $wcscpy->call(\$out1, $string);
101 is($out1, $string, "$name default buffer size");
102
103 my $out2 = "\0" x ($width * (length($string)+1));
104 $wcscpy->call(\$out2, $string);
105 is($out2, $string, "$name with just enough buffer");
106 }
107
108 my $is_null = $ffi->function( pointer_is_null => ['wstring_w'] => 'int' );
109 ok($is_null->call(undef), "NULL");
110 };
111
112 subtest 'wide string as a return value' => sub {
113
114 skip_all 'Test requires wcsdup' unless defined $wcsdup;
115
116 foreach my $test (@strings)
117 {
118 my($name, $string) = @$test;
119 my $ptr = $wcsdup->($string);
120 is($ffi->cast('opaque','wstring', $ptr), $string, $name);
121 }
122
123 is($ffi->cast('opaque','wstring', undef), undef, 'NULL');
124
125 };
126
127 subtest 'wide string as in-out argument' => sub {
128
129 my $wcscat = $ffi->function( wcscat => ['wstring_w','wstring'] );
130 skip_all 'Test requires wcscat' unless defined $wcscat;
131
132 foreach my $test (@strings)
133 {
134 my($name, $string) = @$test;
135
136 my $out1;
137 $wcscat->call([\$out1, $string], $string);
138 is($out1, "$string$string", "$name default buffer size");
139
140 my $out2 = "\0" x ($width * (length($string)*2+1));
141 $wcscat->call([\$out2, $string], $string);
142 is($out2, "$string$string", "$name with just enough buffer");
143 }
144
145 };
146
147 done_testing;