test2.cpp

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C++ 代码 · 共 936 行 · 第 1/2 页

CPP
936
字号
#define BAD
#include <math.h>
#include <stdlib.h>
#include <iostream.h>

#pragma inline_depth 0;

#include "field.h"

# define Assert(ex)	{if (!(ex)){(void)printf("Failed test: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}}

static int flag = 0;	// Used in test for virtuality of destructor

main(){
    #if defined(__386__)
    __chipbug = (__typeof(__chipbug)) ~0;
    #endif
  register i;
  FILE *tmp = tmpfile();

// Default constructor
  {
    ifield a, b;
    Assert(a.Length() == 0);	// Default constructor creates length 0 ifield
    b = ifield(10, 0);		// length 0 ifield may be assigned to
    ifield c;
    Assert(fwrite(b, tmp));
    rewind(tmp);
    Assert(fread(c, tmp));	// length 0 ifield may be fread to
    Assert(c.Length() == 10);
    for (i=0; i<10; i++) 
      Assert(c[i] == 0);
  }

// Constructor with size argument (only)
  {
    ifield a(7);
    Assert(a.Length() == 7);	// Correct number of elements allocated
  }

// Constructor with size and initialization
  {
    ifield a(5, 10);
    Assert(a.Length() == 5);	// Correct length
    for (i=0; i<5; i++) 
      Assert(a[i] = 10);	// Correct data
  }

// Conversion from C array
  {
    static const int data[5] = { 5, 4, 3, 2, 1};
    ifield a(5, data);
    Assert(a.Length() == 5);	// Correct length
    for (i=0; i<5; i++) 
      Assert(a[i] == data[i]);	// Correct data
  }

// Copy constructor
  {
    ifield a(22, 3);
    ifield b(a);
    Assert(b.Length() == 22);	// Correct length
    for (i=0; i<22; i++) 
      Assert(b[i] == 3);	// Correct data
  }

// Conversion from field
  {
    field q(10, 3);
    ifield d = q;
    Assert(d.Length() == 10);	// Correct length
    for (i=0; i<10; i++) 
      Assert(d[i] == 3);	// Correct data
  }

// Is destructor virtual?

  {
    class Derived_Field : public ifield {
      private:
        int *flag;
      public:
        Derived_Field(void) : flag(&::flag) { *flag = 1; }
        ~Derived_Field(void){ *flag = 0; }
    } *derived;
    derived = new Derived_Field;
    ifield *base = derived;
    delete base;
    Assert(flag == 0);		// Was the correct destructor called?
  }

// Test of assignment
  {
    ifield a(7, 2);
    ifield b(3, 10);
    a = b;
    Assert(a.Length() == 3);	// Correct length
    for (i=0; i<3; i++) 
      Assert(a[i] == 10);	// Correct data
  }

// Test of assignment of int
  {
    ifield a(7, 2);
    a = 5;
    Assert(a.Length() == 7);	// Correct length
    for (i=0; i<7; i++) 
      Assert(a[i] == 5);	// Correct data
  }

// Subscript and pointer conversion operators
  {
    ifield a(10, 0);
    ifield b;
    Assert(a[0] == 0);	
    Assert((a[0] = 2, a[0]) == 2);	// Correct access
    Assert(&a[1+2] == &a[1]+2);		// Correct reference
    b = a;
    Assert(&a[0] != &b[0]);		// Uniqueness
    Assert(&a[0] == (int*)a);	// Correspondence
    Assert(&a[3] == (int*)a + 3);
    int& ref = a[0];
    b = a;
    Assert(&ref == &a[0]);
  }

// Operations
  {
    static const int data[] = {0, 1, 2, 3, 4, 5};
    ifield a;
    size_t l = sizeof(data)/sizeof(double);
    a = ifield(l, data); 

// Unary operators
    {
      ifield b = +a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]);

      b = -a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == -a[i]);

      b = ~a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == ~a[i]);

      b = !a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == !a[i]);
    }

// Operators with scalar constants
    {
      ifield b = a*3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3*a[i]);

      b = 3*a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3.0*a[i]);

      b = a;
      b *= 3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3*a[i]);


      b = a/3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]/3);

      b = 3/(a+1);
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3/(a[i]+1));

      b = a;
      b /= 3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]/3);


      b = a%3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]%3);

      b = 3%(a+1);
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3%(a[i]+1));

      b = a;
      b %= 3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]%3);


      b = a+3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]+3);

      b = 3+a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3+a[i]);

      b = a;
      b += 3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]+3);


      b = a-3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]-3);

      b = 3-a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3-a[i]);

      b = a;
      b -= 3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]-3);


      b = a^3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (a[i]^3));

      b = 3^a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (3^a[i]));

      b = a;
      b ^= 3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (a[i]^3));


      b = a&3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (a[i]&3));

      b = 3&a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (3&a[i]));

      b = a;
      b &= 3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (a[i]&3));


      b = a|3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (a[i]|3));

      b = 3|a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (3|a[i]));

      b = a;
      b |= 3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (a[i]|3));


      b = a<3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]<3);

      b = 3<a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3<a[i]);


      b = a>3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]>3);

      b = 3>a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3>a[i]);


      b = a<=3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]<=3);

      b = 3<=a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3<=a[i]);


      b = a>=3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]>=3);

      b = 3>=a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3>=a[i]);


      b = a<<3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]<<3);

      b = 3<<a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3<<a[i]);

      b = a;
      b <<= 3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]<<3);


      b = a>>3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]>>3);

      b = 3>>a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == 3>>a[i]);

      b = a;
      b >>= 3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == a[i]>>3);


      b = a&&3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (a[i]&&3));

      b = 3&&a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (3&&a[i]));


      b = a||3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (a[i]||3));

      b = 3||a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (3||a[i]));


      b = a==3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (a[i]==3));

      b = 3==a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (3==a[i]));


      b = a!=3;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (a[i]!=3));

      b = 3!=a;
      Assert(b.Length() == l);
      for (i=0; i<l; i++)
        Assert(b[i] == (3!=a[i]));
    }

// Operators with other integer ifields
    {
      ifield b = a;
      ifield c;


      c = a * b;
      Assert(c.Length() == l);
      for (i=0; i<l; i++)
        Assert(c[i] == a[i] * b[i]);

      c = a;
      c *= b;
      Assert(c.Length() == l);
      for (i=0; i<l; i++)
        Assert(c[i] == a[i] * b[i]);


      c = a / (b+1);
      Assert(c.Length() == l);
      for (i=0; i<l; i++)
        Assert(c[i] == a[i] / (b[i]+1));

      c = a;
      c /= (b+1);
      Assert(c.Length() == l);
      for (i=0; i<l; i++)
        Assert(c[i] == a[i] / (b[i]+1));


      c = a % (b+1);
      Assert(c.Length() == l);
      for (i=0; i<l; i++)
        Assert(c[i] == a[i] % (b[i]+1));

      c = a;
      c %= (b+1);
      Assert(c.Length() == l);
      for (i=0; i<l; i++)
        Assert(c[i] == a[i] % (b[i]+1));


      c = a + b;
      Assert(c.Length() == l);
      for (i=0; i<l; i++)
        Assert(c[i] == a[i] + b[i]);

      c = a;
      c += b;
      Assert(c.Length() == l);
      for (i=0; i<l; i++)
        Assert(c[i] == a[i] + b[i]);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?