File Coverage

examples/perltypes.pl
Criterion Covered Total %
statement 31 32 96.8
branch 2 4 50.0
condition 2 5 40.0
subroutine 4 4 100.0
pod n/a
total 39 45 86.6


line stmt bran cond sub pod time code
1             #!/usr/local/bin/perl -w
2             ################################################################################
3             #
4             # Copyright (c) 2002-2020 Marcus Holland-Moritz. All rights reserved.
5             # This program is free software; you can redistribute it and/or modify
6             # it under the same terms as Perl itself.
7             #
8             ################################################################################
9              
10             #===============================================================================
11             #
12             # Parse perl's header files and play around with the types they define.
13             #
14             #===============================================================================
15              
16 1     1   794 use Convert::Binary::C;
  1         2  
  1         41  
17 1     1   575 use Data::Dumper;
  1         6194  
  1         74  
18 1     1   450 use File::Spec::Functions qw(rel2abs);
  1         784  
  1         60  
19 1     1   5 use strict;
  1         1  
  1         1537  
20              
21 1         3 my $base;
22 1   33     25 -d "$_/include" and $base = rel2abs("$_/include") and last for qw( tests ../tests );
      50        
23 1 50       50 defined $base or die <
24             Please run this script from either the 'examples' directory
25             or the distribution base directory.
26             MSG
27              
28             #-------------------------------------
29             # Create an object, set configuration.
30             #-------------------------------------
31              
32 1         464 my $cfg = require "$base/config.pl";
33 1         86 my $c = new Convert::Binary::C %$cfg;
34              
35             #------------------
36             # Parse the C file.
37             #------------------
38              
39 1         3 eval { $c->parse_file( "$base/include.c" ) };
  1         15949  
40              
41             #-----------------------
42             # Check for parse error.
43             #-----------------------
44              
45 1 50       25 if( $@ ) {
46 0         0 die "Parse error: $@";
47             }
48              
49             #----------------------------
50             # Dump out the configuration.
51             #----------------------------
52              
53 1         81 print Dumper( $c->configure );
54              
55             #----------------------------
56             # Print all the enumerations.
57             #----------------------------
58              
59 1         422 my @enums = $c->enum_names;
60 1         6 print "\nenums: @enums\n\n";
61              
62             #---------------------------------------------------------------------------
63             # Print all structs, sorted by size; skip all structs smaller than 50 bytes.
64             #---------------------------------------------------------------------------
65              
66 1         1 print "large structs:\n\n";
67              
68 8         24 my @structs = sort { $c->sizeof( $b ) <=> $c->sizeof( $a ) }
69 1         21 grep { $c->sizeof( $_ ) >= 50 }
  19         94  
70             $c->struct_names;
71              
72 1         4 for my $struct ( @structs ) {
73 6         26 printf "struct %-20s => %4d bytes\n", $struct, $c->sizeof( $struct );
74             }
75              
76 1         3 print "\n";
77              
78             #-----------------------------------------------
79             # Dump the definition of the __socket_type enum
80             #-----------------------------------------------
81              
82 1         7 print Data::Dumper->Dump( [$c->enum('__socket_type')], ['__socket_type'] );