summaryrefslogtreecommitdiff
path: root/dist/ExtUtils-ParseXS/t/510-t-bare.t
blob: a2f359d80eab95426f19b8b0757cb2bc2b4f8c7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/perl
use strict;
use warnings;

use Test::More tests => 38;
use ExtUtils::Typemaps;

# typemap only
SCOPE: {
  my $map = ExtUtils::Typemaps->new();
  $map->add_typemap(ctype => 'unsigned int', xstype => 'T_IV');
  is($map->as_string(), <<'HERE', "Simple typemap matches expectations");
TYPEMAP
unsigned int	T_IV
HERE

  my $type = $map->get_typemap(ctype => 'unsigned int');
  isa_ok($type, 'ExtUtils::Typemaps::Type');
  is($type->ctype, 'unsigned int');
  is($type->xstype, 'T_IV');
  is($type->tidy_ctype, 'unsigned int');

  # test failure
  ok(!$map->get_typemap(ctype => 'foo'), "Access to nonexistent typemap doesn't die");
  ok(!$map->get_inputmap(ctype => 'foo'), "Access to nonexistent inputmap via ctype doesn't die");
  ok(!$map->get_outputmap(ctype => 'foo'), "Access to nonexistent outputmap via ctype doesn't die");
  ok(!$map->get_inputmap(xstype => 'foo'), "Access to nonexistent inputmap via xstype doesn't die");
  ok(!$map->get_outputmap(xstype => 'foo'), "Access to nonexistent outputmap via xstype doesn't die");
  ok(!eval{$map->get_typemap('foo')} && $@, "Access to typemap with positional params dies");
  ok(!eval{$map->get_inputmap('foo')} && $@, "Access to inputmap with positional params dies");
  ok(!eval{$map->get_outputmap('foo')} && $@, "Access to outputmap with positional params dies");
}

# typemap & input
SCOPE: {
  my $map = ExtUtils::Typemaps->new();
  $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
  $map->add_inputmap(xstype => 'T_UV', code => '$var = ($type)SvUV($arg);');
  is($map->as_string(), <<'HERE', "Simple typemap (with input) matches expectations");
TYPEMAP
unsigned int	T_UV

INPUT
T_UV
	$var = ($type)SvUV($arg);
HERE

  my $type = $map->get_typemap(ctype => 'unsigned int');
  isa_ok($type, 'ExtUtils::Typemaps::Type');
  is($type->ctype, 'unsigned int');
  is($type->xstype, 'T_UV');
  is($type->tidy_ctype, 'unsigned int');

  my $in = $map->get_inputmap(xstype => 'T_UV');
  isa_ok($in, 'ExtUtils::Typemaps::InputMap');
  is($in->xstype, 'T_UV');

  # test fetching inputmap by ctype
  my $in2 = $map->get_inputmap(ctype => 'unsigned int');
  is_deeply($in2, $in, "get_inputmap returns the same typemap for ctype and xstype");
}


# typemap & output
SCOPE: {
  my $map = ExtUtils::Typemaps->new();
  $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
  $map->add_outputmap(xstype => 'T_UV', code => 'sv_setuv($arg, (UV)$var);');
  is($map->as_string(), <<'HERE', "Simple typemap (with output) matches expectations");
TYPEMAP
unsigned int	T_UV

OUTPUT
T_UV
	sv_setuv($arg, (UV)$var);
HERE

  my $type = $map->get_typemap(ctype => 'unsigned int');
  isa_ok($type, 'ExtUtils::Typemaps::Type');
  is($type->ctype, 'unsigned int');
  is($type->xstype, 'T_UV');
  is($type->tidy_ctype, 'unsigned int');

  my $in = $map->get_outputmap(xstype => 'T_UV');
  isa_ok($in, 'ExtUtils::Typemaps::OutputMap');
  is($in->xstype, 'T_UV');
}

# typemap & input & output
SCOPE: {
  my $map = ExtUtils::Typemaps->new();
  $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
  $map->add_inputmap(xstype => 'T_UV', code => '$var = ($type)SvUV($arg);');
  $map->add_outputmap(xstype => 'T_UV', code => 'sv_setuv($arg, (UV)$var);');
  is($map->as_string(), <<'HERE', "Simple typemap (with in- & output) matches expectations");
TYPEMAP
unsigned int	T_UV

INPUT
T_UV
	$var = ($type)SvUV($arg);

OUTPUT
T_UV
	sv_setuv($arg, (UV)$var);
HERE
}

# two typemaps & input & output
SCOPE: {
  my $map = ExtUtils::Typemaps->new();
  $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
  $map->add_inputmap(xstype => 'T_UV', code => '$var = ($type)SvUV($arg);');
  $map->add_outputmap(xstype => 'T_UV', code => 'sv_setuv($arg, (UV)$var);');

  $map->add_typemap(ctype => 'int', xstype => 'T_IV');
  $map->add_inputmap(xstype => 'T_IV', code => '$var = ($type)SvIV($arg);');
  $map->add_outputmap(xstype => 'T_IV', code => 'sv_setiv($arg, (IV)$var);');
  is($map->as_string(), <<'HERE', "Simple typemap (with in- & output) matches expectations");
TYPEMAP
unsigned int	T_UV
int	T_IV

INPUT
T_UV
	$var = ($type)SvUV($arg);
T_IV
	$var = ($type)SvIV($arg);

OUTPUT
T_UV
	sv_setuv($arg, (UV)$var);
T_IV
	sv_setiv($arg, (IV)$var);
HERE
  my $type = $map->get_typemap(ctype => 'unsigned int');
  isa_ok($type, 'ExtUtils::Typemaps::Type');
  is($type->ctype, 'unsigned int');
  is($type->xstype, 'T_UV');
  is($type->tidy_ctype, 'unsigned int');

  my $in = $map->get_outputmap(xstype => 'T_UV');
  isa_ok($in, 'ExtUtils::Typemaps::OutputMap');
  is($in->xstype, 'T_UV');
  $in = $map->get_outputmap(xstype => 'T_IV');
  isa_ok($in, 'ExtUtils::Typemaps::OutputMap');
  is($in->xstype, 'T_IV');
}