Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ Timesaving Techniques (2005) [eng].pdf
Скачиваний:
65
Добавлен:
16.08.2013
Размер:
8.35 Mб
Скачать

146 Technique 26: Doing Data Validation with Classes

5. Save your code and close the code editor.

The file we just defined will be a real type that you can use in your own applications to store and validate Social Security Numbers (SSNs). You will never again have to write code to check the length of an entry or its contents to see whether it could be a valid SSN value.

Create a new type for every kind of data you will accept and process in your application. Create a validator for the data type that can be moved from project to project.

Note that we provided constants for both the length of the SSN and its delimiter (see lines marked 1 and 2). This allows you to easily modify the code if the format of the SSN changes over time. Someday you may need to change the SSN to use more digits, or to be formatted with a different delimiter. Preparing for this now saves you huge amounts of time later.

Never hard-code values into your applications; always use constants that can be easily changed at compile-time.

Testing Your SSN Validator Class

After you create the Validator class, you should create a test driver to ensure that your code is correct and show people how to use your code.

Creating a test driver will illustrate the validation of various kinds of input from the user, and will show how the Validator class is intended to be used. The driver will contain some basic tests of the class, as well as accepting Social Security Numbers from the user to see whether they are valid or not.

In this example, we create a test driver that does two things. First, it creates a standard battery of tests that illustrates the expected good and bad entries for the type. Second, the test driver allows the programmer to try other styles of entry to see whether the class catches them.

1. In the code editor of your choice, reopen the file to hold the code for your test program.

In this example, I named the test program ch26.cpp.

2. Append the code from Listing 26-3 into your test driver file, substituting the names you used for your SSN class definition where appropriate.

Better yet, copy the code you find from the source file on this book’s companion Web site.

LISTING 26-3: THE TEST DRIVER CODE

const char *TrueOrFalse( bool value )

{

if ( value ) return “TRUE”;

return “FALSE”;

}

void DoValidTest( const char *strName, SSNValidator& ssn, bool expected_result )

{

bool bValid = ssn.Valid();

 

 

printf(“%s: Result %s. Expected

Result: %s. %s\n”, strName,

 

TrueOrFalse( bValid ), TrueOrFalse( expected_result ),

 

( bValid == expected_result

? “PASS” : “FAIL” )

);

}

Testing Your SSN Validator Class

147

int main(int argc, char **argv)

{

if ( argc < 2 )

{

printf(“Usage: ch3_15 ssn1 [ssn2]...\n”); exit(1);

}

for ( int i=1; i<argc; ++i )

{

SSNValidator ssn(argv[i]); if ( ssn.Valid() )

printf(“%s is a valid Social Security Number\n”, ssn.SSN().c_str() );

else

printf(“%s is NOT a valid Social Security Number\n”, argv[i] );

}

// Do some generic testing. SSNValidator ssnNULL( NULL );

DoValidTest( “NULL Test”, ssnNULL, false ); SSNValidator ssnGood(“000-00-0000”); DoValidTest( “Good Test”, ssnGood, true ); SSNValidator ssnBad(“0000a0000”); DoValidTest( “Bad Test”, ssnBad, false );

return 0;

}

3. Save your test driver file in the code editor and close the code-editor program.

4. Compile the test program with your chosen compiler and run it on your chosen operating system.

Enter command-line arguments, such as

123456789 000-00-0000 0909 a12345678

012-03-3456

These are simply forms of the Social Security Number, some valid and some invalid. The first one, containing nine digits and no alphanumeric characters, will be valid. The third argument does not contain nine characters and is therefore invalid. The fourth contains an invalid character (a). The second and fifth entries look valid, but we do not handle the dash character, so they will be deemed invalid by the program.

If your program is working properly, you should see the output from the test driver as shown in Listing 26-4.

LISTING 26-4: OUTPUT FROM THE TEST DRIVER

$ ./a 123456789 000-00-000 0909 a12345678 01-02-2345

123456789 is a valid Social Security Number 000-00-000 is NOT a valid Social Security

Number

0909 is NOT a valid Social Security Number

a12345678 is NOT a valid Social Security Number

01-02-2345 is NOT a valid Social Security Number

NULL Test: Result FALSE. Expected Result: FALSE. PASS

Good Test: Result TRUE. Expected Result: TRUE. PASS

Bad Test: Result FALSE. Expected Result: FALSE. PASS

148 Technique 26: Doing Data Validation with Classes

As you can see by the output, the program first checks the input arguments from the user. As we expected, only the first input value was valid. All the remaining entries were invalid. The remaining tests simply validate that known conditions work properly.

I recommend that you create generic test drivers for all your validators, so when changes are made to accommodate new formats, the drivers will be prepared in advance to test them.

This will save a lot of time in the long run, and will allow for automated testing.

27 Building a Date

Class

Technique

Save Time By

Creating a generic Date class

Implementing date functionality into your class

Testing your class

One of the most common tasks that you will run into as a programmer is working with dates. Whether you are calculating when something is bought or sold, or validating input from the user, your

application will probably need to support dates. The standard C library contains various routines for working with dates, such as the time and localtime functions. The problem, however, is that these routines do not perform adequate validation — and for that matter, they are not easy to use. It would be nice, therefore, to create a single class that implemented all the date functionality that we wanted in our applications. By creating a single class that can be easily ported from project to project, you will save time in the development, design, and testing phases of the project.

Because dates are a fundamental building block of our applications, it makes sense to create a single class that would manipulate them and validate them. If you were to make a list of all of the basic functionality you would like in such a class, you would probably have something like this:

Validate dates

Perform date math calculations

Compute the day of the week

Return day and month names

Convert numeric dates to strings

Many other functions exist that would be useful, but these are the most critical in any application. In this technique, we look at the ways you can utilize a Date class in your own applications — and how you can implement the functionality needed to do everything on our feature list for a Date class.

You can save huge amounts of time by creating classes that not only validate input, but also manipulate it numerically. By creating a class that allows you to add to, or subtract from, a date in your code directly, you do accounting calculations and timing routines in a flash, without any additional coding.