File: parse_number.cf

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (98 lines) | stat: -rw-r--r-- 3,362 bytes parent folder | download | duplicates (7)
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
#######################################################
#
# Test basic interpretation of digits as numbers.
#
#######################################################

body common control
{
      inputs => { "../../default.cf.sub" };
      bundlesequence  => { default("$(this.promise_filename)") };
      version => "1.0";
}

#######################################################

bundle agent init
{
  vars:
      "digits" string => "42";
      "zero"   string => "00000";
      "negate" string => "-137";
      "spacey" string => "17 
	
"; # Spaces ignored after number
      "kilo"   string => "42k";
      "kibi"   string => "42K";
      "mega"   string => "42m";
      "mibi"   string => "42M";
      # NB: 42g fails on 32-bit - many places go via int, which overflows (Redmine 4404).
      "giga"   string => "1g";
      "gibi"   string => "1G";
      "pi"     string => "3.141592653589793238462643383279502884197169399375105820974";
      "nowt"   string => "-000.0000e137";
      "nogiga" string => "0e0G   "; # quantifier and spaces
      "me2pi"  string => "-.5354916555247646e-3M"; # minus exp(two pi)

      "whole" slist => { "digits", "zero", "negate", "spacey",
                         "kilo", "kibi", "mega", "mibi", "giga", "gibi" };
      "float" slist => { "pi", "nowt", "nogiga", "me2pi" };

      # Expected answers:
      "round[digits]" string => "$(digits)";
      "round[zero]"   string => "0";
      "round[negate]" string => "$(negate)";
      "round[spacey]" string => "17";
      "round[kilo]"   string => "42000";
      "round[kibi]"   string => "43008";
      "round[mega]"   string => "42000000";
      "round[mibi]"   string => "44040192";
      "round[giga]"   string => "1000000000";
      "round[gibi]"   string => "1073741824";
    "ratio[$(whole)]" string => "$(round[$(whole)]).000000";
      "ratio[pi]"     string => "3.14159265";
      "ratio[nowt]"   string => "$(ratio[zero])";
      "ratio[nogiga]" string => "$(ratio[zero])";
      "ratio[me2pi]"  string => "535.491656";
}

#######################################################

bundle agent test
{
  vars:
      "round[$(init.whole)]"  int => "$(init.$(init.whole))";
      "ratio[$(init.whole)]" real => "$(init.$(init.whole))";
      "ratio[$(init.float)]" real => "$(init.$(init.float))";
}


#######################################################

bundle agent check
{
  vars:
      "float" slist => { @(init.whole), @(init.float) };

  classes:
      "whole_$(init.whole)" expression => strcmp("$(test.round[$(init.whole)])",
                                                 "$(init.round[$(init.whole)])");
      "float_$(float)" expression => strcmp("$(test.ratio[$(float)])",
                                            "$(init.ratio[$(float)])");
      "$(init.whole)" and => { "whole_$(init.whole)", "float_$(init.whole)" };
      "$(init.float)" and => { "float_$(init.whole)" };
      "iok" and => { @(init.whole) };
      "rok" and => { @(init.float) };
      "ok" and => { "iok", "rok" };

  reports:
    DEBUG.!iok::
      "Read $(init.$(init.whole)) as int $(test.round[$(init.whole)]), expected $(init.round[$(init.whole)])";
    DEBUG.!rok::
      "Read $(init.$(float)) as real $(test.ratio[$(float)]), expected $(init.ratio[$(float)])";

    ok::
      "$(this.promise_filename) Pass";
    !ok::
      "$(this.promise_filename) FAIL";
}