File: 241_sizeof.t

package info (click to toggle)
libconvert-binary-c-perl 0.74-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 9,100 kB
  • ctags: 21,416
  • sloc: ansic: 63,666; perl: 18,582; yacc: 2,143; makefile: 44
file content (84 lines) | stat: -rw-r--r-- 2,284 bytes parent folder | download
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
################################################################################
#
# $Project: /Convert-Binary-C $
# $Author: mhx $
# $Date: 2009/03/15 04:10:54 +0100 $
# $Revision: 4 $
# $Source: /tests/241_sizeof.t $
#
################################################################################
#
# Copyright (c) 2002-2009 Marcus Holland-Moritz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
################################################################################

use Test::More tests => 71;
use Convert::Binary::C @ARGV;

my $c = new Convert::Binary::C IntSize => 4, CharSize => 1, Alignment => 1;

eval {
$c->parse(<<'ENDC');

struct normal {
  int  a;
  char b[3];
  char c[3][3][3];
};

struct flexible {
  int  a;
  char b[];
};

ENDC
};

is($@, '', "parse C code");

my @tests = (
  [ 'normal.a'             => 4 ],
  [ 'normal.b'             => 3 ],
  [ 'normal.b[0]'          => 1 ],
  [ 'normal.b[1]'          => 1 ],
  [ 'normal.b[2]'          => 1 ],
  [ 'normal.b[3]'          => 1 ],
  [ 'normal.b[4]'          => 1 ],
  [ 'normal.b[+4]'         => 1 ],
  [ 'normal.b[+1000000]'   => 1 ],
  [ 'normal.b[-0]'         => 1 ],
  [ 'normal.b[-1]'         => 1 ],
  [ 'normal.b[-2]'         => 1 ],
  [ 'normal.b[-3]'         => 1 ],
  [ 'normal.b[-4]'         => 1 ],
  [ 'normal.b[-5]'         => 1 ],
  [ 'normal.b[-1000000]'   => 1 ],
  [ 'normal.c[-10]'        => 9 ],
  [ 'normal.c[-10][-10]'   => 3 ],
  [ 'normal.c[-9][-9][-9]' => 1 ],

  [ 'flexible.a'           => 4 ],
  [ 'flexible.b'           => 0 ],
  [ 'flexible.b[0]'        => 1 ],
  [ 'flexible.b[1]'        => 1 ],
  [ 'flexible.b[2]'        => 1 ],
  [ 'flexible.b[3]'        => 1 ],
  [ 'flexible.b[4]'        => 1 ],
  [ 'flexible.b[+4]'       => 1 ],
  [ 'flexible.b[+1000000]' => 1 ],
  [ 'flexible.b[-0]'       => 1 ],
  [ 'flexible.b[-1]'       => 1 ],
  [ 'flexible.b[-2]'       => 1 ],
  [ 'flexible.b[-3]'       => 1 ],
  [ 'flexible.b[-4]'       => 1 ],
  [ 'flexible.b[-5]'       => 1 ],
  [ 'flexible.b[-1000000]' => 1 ],
);

for my $t (@tests) {
  my $size = eval { $c->sizeof($t->[0]) };
  is($@, '', "eval { sizeof($t->[0]) }");
  is($size, $t->[1], "sizeof($t->[0]) == $t->[1]");
}