Home | History | Annotate | Download | only in src
      1 // Copyright 2008 The Android Open Source Project
      2 
      3 
      4 
      5 /**
      6  * Test comparison operators.
      7  */
      8 public class Compare {
      9 
     10     /*
     11      * Test the integer comparisons in various ways.
     12      */
     13     static void testIntCompare(int minus, int plus, int plus2, int zero) {
     14         System.out.println("IntMath.testIntCompare");
     15 
     16         if (minus > plus)
     17             assert(false);
     18         if (minus >= plus)
     19             assert(false);
     20         if (plus < minus)
     21             assert(false);
     22         if (plus <= minus)
     23             assert(false);
     24         if (plus == minus)
     25             assert(false);
     26         if (plus != plus2)
     27             assert(false);
     28 
     29         /* try a branch-taken */
     30         if (plus != minus) {
     31             assert(true);
     32         } else {
     33             assert(false);
     34         }
     35 
     36         if (minus > 0)
     37             assert(false);
     38         if (minus >= 0)
     39             assert(false);
     40         if (plus < 0)
     41             assert(false);
     42         if (plus <= 0)
     43             assert(false);
     44         if (plus == 0)
     45             assert(false);
     46         if (zero != 0)
     47             assert(false);
     48 
     49         if (zero == 0) {
     50             assert(true);
     51         } else {
     52             assert(false);
     53         }
     54     }
     55 
     56     /*
     57      * Test cmp-long.
     58      *
     59      * minus=-5, alsoMinus=0xFFFFFFFF00000009, plus=4, alsoPlus=8
     60      */
     61     static void testLongCompare(long minus, long alsoMinus, long plus,
     62         long alsoPlus) {
     63 
     64         System.out.println("IntMath.testLongCompare");
     65         if (minus > plus)
     66             assert(false);
     67         if (plus < minus)
     68             assert(false);
     69         if (plus == minus)
     70             assert(false);
     71 
     72         if (plus >= plus+1)
     73             assert(false);
     74         if (minus >= minus+1)
     75             assert(false);
     76 
     77         /* try a branch-taken */
     78         if (plus != minus) {
     79             assert(true);
     80         } else {
     81             assert(false);
     82         }
     83 
     84         /* compare when high words are equal but low words differ */
     85         if (plus > alsoPlus)
     86             assert(false);
     87         if (alsoPlus < plus)
     88             assert(false);
     89         if (alsoPlus == plus)
     90             assert(false);
     91 
     92         /* high words are equal, low words have apparently different signs */
     93         if (minus < alsoMinus)      // bug!
     94             assert(false);
     95         if (alsoMinus > minus)
     96             assert(false);
     97         if (alsoMinus == minus)
     98             assert(false);
     99     }
    100 
    101     /*
    102      * Test cmpl-float and cmpg-float.
    103      */
    104     static void testFloatCompare(float minus, float plus, float plus2,
    105         float nan) {
    106 
    107         System.out.println("IntMath.testFloatCompare");
    108         if (minus > plus)
    109             assert(false);
    110         if (plus < minus)
    111             assert(false);
    112         if (plus == minus)
    113             assert(false);
    114         if (plus != plus2)
    115             assert(false);
    116 
    117         if (plus <= nan)
    118             assert(false);
    119         if (plus >= nan)
    120             assert(false);
    121         if (minus <= nan)
    122             assert(false);
    123         if (minus >= nan)
    124             assert(false);
    125         if (nan >= plus)
    126             assert(false);
    127         if (nan <= plus)
    128             assert(false);
    129 
    130         if (nan == nan)
    131             assert(false);
    132     }
    133 
    134     static void testDoubleCompare(double minus, double plus, double plus2,
    135         double nan) {
    136 
    137         System.out.println("IntMath.testDoubleCompare");
    138         if (minus > plus)
    139             assert(false);
    140         if (plus < minus)
    141             assert(false);
    142         if (plus == minus)
    143             assert(false);
    144         if (plus != plus2)
    145             assert(false);
    146 
    147         if (plus <= nan)
    148             assert(false);
    149         if (plus >= nan)
    150             assert(false);
    151         if (minus <= nan)
    152             assert(false);
    153         if (minus >= nan)
    154             assert(false);
    155         if (nan >= plus)
    156             assert(false);
    157         if (nan <= plus)
    158             assert(false);
    159 
    160         if (nan == nan)
    161             assert(false);
    162     }
    163 
    164     public static void run() {
    165         testIntCompare(-5, 4, 4, 0);
    166         testLongCompare(-5L, -4294967287L, 4L, 8L);
    167 
    168         testFloatCompare(-5.0f, 4.0f, 4.0f, (1.0f/0.0f) / (1.0f/0.0f));
    169         testDoubleCompare(-5.0, 4.0, 4.0, (1.0/0.0) / (1.0/0.0));
    170     }
    171 }
    172