File: 11token_float.t

package info (click to toggle)
libparser-mgc-perl 0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 536 kB
  • sloc: perl: 1,881; makefile: 2; sh: 1
file content (47 lines) | stat: -rw-r--r-- 1,406 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
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

package TestParser {
   use base qw( Parser::MGC );

   sub parse
   {
      my $self = shift;

      return $self->token_float;
   }
}

my $parser = TestParser->new;

# We're going to be testing floating point values.
sub approx
{
   my ( $got, $exp, $name ) = @_;

   ok( abs( $got - $exp ) < 1E-12, $name ) or
      diag( "Expected approximately $exp, got $got" );
}

approx( $parser->from_string( "123.0" ), 123,    'Decimal integer' );
approx( $parser->from_string( "0.0" ),     0,    'Zero' );
approx( $parser->from_string( "12." ),    12,    'Trailing DP' );
approx( $parser->from_string( ".34" ),     0.34, 'Leading DP' );
approx( $parser->from_string( "8.9" ),     8.9,  'Infix DP' );

approx( $parser->from_string( "-4.0" ),   -4,    'Negative decimal' );

approx( $parser->from_string( "1E0" ),     1, 'Scientific without DP' );
approx( $parser->from_string( "2.0E0" ),   2, 'Scientific with DP' );
approx( $parser->from_string( "3.E0" ),    3, 'Scientific with trailing DP' );
approx( $parser->from_string( ".4E1" ),    4, 'Scientific with leading DP' );
approx( $parser->from_string( "50E-1" ),   5, 'Scientific with negative exponent without DP' );
approx( $parser->from_string( "60.0E-1" ), 6, 'Scientific with DP with negative exponent' );

approx( $parser->from_string( "1e0" ), 1, 'Scientific with lowercase e' );

done_testing;