home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / gplibt02 / tfix16.cc < prev    next >
C/C++ Source or Header  |  1993-07-24  |  3KB  |  110 lines

  1. //
  2. // testFix16.cc : test Fix16/32 classes
  3. //
  4.  
  5. #include <xfix16.h>
  6.  
  7. // This set of inlines (instead of a macro) is to force the side effects
  8. // of evaluating y to happen before x is printed.
  9.  
  10. inline void check(char *x, int y) { cout << x << " = " << (y) << "\n"; }
  11. inline void check(char *x, double y) { cout << x << " = " << (y) << "\n"; }
  12. inline void check(char *x, Fix16 y) { cout << x << " = " << (y) << "\n"; }
  13. inline void check(char *x, Fix32 y) { cout << x << " = " << (y) << "\n"; }
  14.  
  15. void test16() {
  16.   cout << "Fix16: identities should be displayed\n";
  17.  
  18.   Fix16 a;        check("0",a);
  19.   Fix16 b = .5;        check(".5",b);
  20.   Fix16 c = -.5;    check("-.5",c);
  21.   Fix16 d = .1;        check(".1",d);
  22.   Fix16 e = b;        check(".5",e);
  23.  
  24.   check(".5",a = b);
  25.   check(".25",a = .25);
  26.   check("8192",mantissa(a));
  27.   mantissa(a)=8192;
  28.   check(".25",a);
  29.   check(".25",value(a));
  30.  
  31.   check(".25",+a);
  32.   check("-.25",-a);
  33.  
  34.   check(".1 + .5",d+b);
  35.   check(".1 - .5",d-b);
  36.   check(".1 * .5",d*b);
  37.   check(".1 *  3",d*3);
  38.   check(".1 * -3",d*-3);
  39.   check(".1 / .5",d/b);
  40.   check(".1 << 1",d<<1);
  41.   check("-.5 >> 2",c>>2);
  42.  
  43.   check(".1 == .5",d == b);
  44.   check(".1 != .5",d != b);
  45.   check(".1 > .5",d > b);
  46.   check(".5 <= -.5",b <= c);
  47.  
  48.   cout << "Fix16: range errors ignored and overflows saturated\n";
  49.   set_Fix16_overflow_handler(Fix16_overflow_saturate);
  50.   set_Fix16_range_error_handler(Fix16_ignore);
  51.  
  52.   Fix16 f = 1.1;    check("1.1",f);
  53.  
  54.   Fix16 g = .7;
  55.   check(".7 + .5",g+b);
  56.   check("-.5 - .7",c-g);
  57.   check(".5 / .1",b/d);
  58. }
  59.  
  60. void test32() {
  61.   cout << "Fix32: identities should be displayed\n";
  62.  
  63.   Fix32 a;        check("0",a);
  64.   Fix32 b = .5;        check(".5",b);
  65.   Fix32 c = -.5;    check("-.5",c);
  66.   Fix32 d = .1;        check(".1",d);
  67.   Fix32 e = b;        check(".5",e);
  68.  
  69.   check(".5",a = b);
  70.   check(".25",a = .25);
  71.   check("536870912",mantissa(a));
  72.   mantissa(a)=536870912;
  73.   check(".25",a);
  74.   check(".25",value(a));
  75.  
  76.   check(".25",+a);
  77.   check("-.25",-a);
  78.  
  79.   check(".1 + .5",d+b);
  80.   check(".1 - .5",d-b);
  81.   check(".1 * .5",d*b);
  82.   check(".1 *  3",d*3);
  83.   check(".1 * -3",d*-3);
  84.   check(".1 / .5",d/b);
  85.   check(".1 << 1",d<<1);
  86.   check("-.5 >> 2",c>>2);
  87.  
  88.   check(".1 == .5",d == b);
  89.   check(".1 != .5",d != b);
  90.   check(".1 > .5",d > b);
  91.   check(".5 <= -.5",b <= c);
  92.  
  93.   cout << "Fix32: range errors reported and overflows reported\n";
  94.   set_Fix32_overflow_handler(Fix32_warning);
  95.   set_Fix32_range_error_handler(Fix32_warning);
  96.  
  97.   Fix32 f = 1.1;    check("1.1",f);
  98.  
  99.   Fix32 g = .7;
  100.   check(".7 + .5",g+b);
  101.   check("-.5 - .7",c-g);
  102.   check(".5 / .1",b/d);
  103. }
  104.  
  105. main() {
  106.   test16();
  107.   test32();
  108. }
  109.  
  110.