Tutorial 6 - global variables, functions and classes

// tutorial6.ot
group "tutorial_6";

global
{
  #define PI 3.14

  int a = 2;
  int b = 4;

  int Difference(int val1, int val2)
  {
    return val1 - val2;
  }

  class Circle
  {
  public:
    Circle(float radius);
	float GetRadius();
	float GetArea();
  private:
    float radius;
  };

  Circle::Circle(float radius) : radius(radius) {}

  float Circle::GetRadius()
  {
    return radius;
  }

  float Circle::GetArea()
  {
    return PI * radius*radius;
  }
}

test("Check if GetArea seems valid")
{
  // GetArea seems valid if the circle with the largest radius also has the largest area.
  Circle c1(3);
  Circle c2(2);
  if (c1.GetRadius() > c2.GetRadius())
  {
    verify(c1.GetArea() > c2.GetArea());
  }
  else
  {
    verify(c2.GetArea() >= c1.GetArea());
  }
}

test("Check if GetArea seems sane")
{
  // Also, the area should be more than twice as large if the radius doubles.
  Circle c1(2);
  Circle c2(4);
  verify(c1.GetArea()*2 < c2.GetArea());
}

In this example, the global keyword is introduced. Inside the global scope surrounded by curly braces, you can use all the normal C++ expressions that you can think of, like variables, functions, and classes. The global section is used from the tests. Another thing to notice is that in this test, we have 2 verify operations. That is completely ok, you can have as many verify operations as you like.

=========================================================================
Opera Testsuite
=========================================================================

=========================================================================
E:/src/operaclean/opera-windows7/modules/selftest/documentation/tutorial/Kopia av tutorial6.ot(2):  tutorial_6 
=========================================================================
  Check if GetArea seems valid .................................. Passed
  Check if GetArea seems sane ................................... Passed
=========================================================================
2 tests run, 0 tests failed, 0 tests skipped, 2 tests OK
=========================================================================