Читаем Thinking In C++. Volume 2: Practical Programming полностью

In this trivial case, the function test( ) maintains the global variables nPass and nFail. The only visual inspection you do is to read the final score. If a test failed, a more sophisticated test( ) displays an appropriate message. The framework described later in this chapter has such a test function, among other things.

You can now implement enough of the Date class to get these tests to pass, and then you can proceed iteratively in like fashion until all the requirements are met. By writing tests first, you are more likely to think of corner cases that might break your upcoming implementation, and you’re more likely to write the code correctly the first time. Such an exercise might produce the following "final" version of a test for the Date class:.

//: C02:SimpleDateTest2.cpp

//{L} Date

#include

#include "Date.h"

using namespace std;

// Test machinery

int nPass = 0, nFail = 0;

void test(bool t) {

  if(t) nPass++; else nFail++;

}

int main() {

  Date mybday(1951, 10, 1);

  Date today;

Date myevebday("19510930");

  // Test the operators

  test(mybday < today);

  test(mybday <= today);

  test(mybday != today);

  test(mybday == mybday);

  test(mybday >= mybday);

  test(mybday <= mybday);

  test(myevebday < mybday);

  test(mybday > myevebday);

  test(mybday >= myevebday);

  test(mybday != myevebday);

  // Test the functions

  test(mybday.getYear() == 1951);

  test(mybday.getMonth() == 10);

  test(mybday.getDay() == 1);

  test(myevebday.getYear() == 1951);

  test(myevebday.getMonth() == 9);

  test(myevebday.getDay() == 30);

  test(mybday.toString() == "19511001");

  test(myevebday.toString() == "19510930");

  // Test duration

  Date d2(2003, 7, 4);

  Date::Duration dur = duration(mybday, d2);

  test(dur.years == 51);

  test(dur.months == 9);

  test(dur.days == 3);

  // Report results:

  cout << "Passed: " << nPass << ", Failed: "

       << nFail << endl;

} ///:~

The word "final" above was quoted because this test can of course be more fully developed. For example we haven’t tested that long durations are handled correctly. To save space on the printed page we’ll stop here, but you get the idea. The full implementation for the Date class is available in the files Date.h and Date.cpp in the appendix and on the MindView website.[21][ ]

<p>The TestSuite Framework</p>

Some automated C++ unit test tools are available on the World Wide Web for download, such as CppUnit.[22] These are well designed and implemented, but our purpose here is not only to present a test mechanism that is easy to use, but also easy to understand internally and even tweak if necessary. So, in the spirit of "TheSimplestThingThatCouldPossiblyWork," we have developed the TestSuite Framework, a namespace named TestSuite that contains two key classes: Test and Suite.

The Test class is an abstract class you derive from to define a test object. It keeps track of the number of passes and failures for you and displays the text of any test condition that fails. Your main task in defining a test is simply to override the run( ) member function, which should in turn call the test_( ) macro for each Boolean test condition you define.

To define a test for the Date class using the framework, you can inherit from Test as shown in the following program:

//: C02:DateTest.h

#ifndef DATE_TEST_H

#define DATE_TEST_H

#include "Date.h"

#include "../TestSuite/Test.h"

class DateTest : public TestSuite::Test {

  Date mybday;

  Date today;

  Date myevebday;

public:

  DateTest() : mybday(1951, 10, 1), myevebday("19510930") {

  }

  void run() {

    testOps();

    testFunctions();

    testDuration();

  }

  void testOps() {

    test_(mybday < today);

    test_(mybday <= today);

    test_(mybday != today);

    test_(mybday == mybday);

    test_(mybday >= mybday);

    test_(mybday <= mybday);

    test_(myevebday < mybday);

    test_(mybday > myevebday);

    test_(mybday >= myevebday);

    test_(mybday != myevebday);

  }

  void testFunctions() {

    test_(mybday.getYear() == 1951);

    test_(mybday.getMonth() == 10);

    test_(mybday.getDay() == 1);

    test_(myevebday.getYear() == 1951);

    test_(myevebday.getMonth() == 9);

    test_(myevebday.getDay() == 30);

    test_(mybday.toString() == "19511001");

    test_(myevebday.toString() == "19510930");

  }

  void testDuration() {

    Date d2(2003, 7, 4);

    Date::Duration dur = duration(mybday, d2);

    test_(dur.years == 51);

    test_(dur.months == 9);

    test_(dur.days == 3);

  }

};

#endif ///:~

Running the test is a simple matter of instantiating a DateTest object and calling its run( ) member function.

//: C02:DateTest.cpp

// Automated Testing (with a Framework)

//{L} Date ../TestSuite/Test

#include

#include "DateTest.h"

using namespace std;

int main() {

  DateTest test;

  test.run();

  return test.report();

}

/* Output:

Test "DateTest":

        Passed: 21,      Failed: 0

*/ ///:~

The Test::report( ) function displays the previous output and returns the number of failures, so it is suitable to use as a return value from main( ).

Перейти на страницу:

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных