summaryrefslogtreecommitdiff
path: root/tests/lang/operators/operator_spaceship_basic.phpt
blob: 1014c46191f124c3937b1bb71c90eb44255facdb (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
--TEST--
Test <=> operator : different types
--FILE--
<?php
$valid_true = array(1, "1", "true", 1.0, array(1));
$valid_false = array(0, "", 0.0, array(), NULL);

$int1 = 679;
$int2 = -67835;
$valid_int1 = array("678", "678abc", " 678", "678  ", 678.0, 6.789E2, "+678", +678);
$valid_int2 = array("-67836", " -67836", -67835.0001, -6.78351E4);
$invalid_int1 = array(679, "679");
$invalid_int2 = array(-67835, "-67835", "-67836abc", "-67836  ");

$float1 = 57385.45835;
$float2 = -67345.76567;
$valid_float1 = array("57385.45834",  "57385.45834aaa", "  57385.45834", 5.738545834e4);
$valid_float2 = array("-67345.76568", "  -67345.76568", -6.734576568E4);
$invalid_float1 = array(57385.45835, 5.738545835e4);
$invalid_float2 = array(-67345.76567, -6.734576567E4, "-67345.76568aaa");


$toCompare = array(
// boolean test will result in both sides being converted to boolean so !0 = true and true is not > true for example
// also note that a string of "0" is converted to false but a string of "0.0" is converted to true
// false cannot be tested as 0 can never be > 0 or 1
  true, $valid_false, $valid_true,
  $int1, $valid_int1, $invalid_int1,
  $int2, $valid_int2, $invalid_int2,
  $float1, $valid_float1, $invalid_float1,
  $float2, $valid_float2, $invalid_float2
);

$failed = false;
for ($i = 0; $i < count($toCompare); $i +=3) {
   $typeToTest = $toCompare[$i];
   $valid_compares = $toCompare[$i + 1];
   $invalid_compares = $toCompare[$i + 2];

   foreach($valid_compares as $compareVal) {
      if (($typeToTest <=> $compareVal) === 1) {
         // do nothing
      }
      else {
         echo "FAILED: ('$typeToTest' <=> '$compareVal') !== 1\n";
         $failed = true;
      }
      if (($compareVal <=> $typeToTest) === -1) {
         // do nothing
      }
      else {
         echo "FAILED: ('$compareVal' <=> '$typeToTest') !== -1\n";
         $failed = true;
      }
      if (($compareVal <=> $compareVal) === 0) {
         // do nothing
      }
      else {
         echo "FAILED: ('$compareVal' <=> '$compareVal') !== 0\n";
         $failed = true;
      }
   }

   foreach($invalid_compares as $compareVal) {
      if (($typeToTest <=> $compareVal) === 1) {
         echo "FAILED: ('$typeToTest' <=> '$compareVal') === 1\n";
         $failed = true;
      }
      if (($compareVal <=> $typeToTest) === -1) {
         echo "FAILED: ('$compareVal' <=> '$typeToTest') === -1\n";
         $failed = true;
      }
      if (($compareVal <=> $compareVal) !== 0) {
         echo "FAILED: ('$compareVal' <=> '$compareVal') !== 0\n";
         $failed = true;
      }
   }

   if (($typeToTest <=> $typeToTest) === 0) {
      // do nothing
   }
   else {
      echo "FAILED: ('$typeToTest' <=> '$typeToTest') !== 0\n";
      $failed = true;
   }
}
if ($failed == false) {
   echo "Test Passed\n";
}
?>
--EXPECT--
Test Passed