Test-Driven Development Exercise
Task 1 Solution
So how did we TDD task 1? Click on the tabs below to find out...
Here is the first in a series of videos showing how we applied TDD to the problem at hand. The video is about 13 minutes long.
#ifndef ACCOUNT_H_
#define ACCOUNT_H_typedef struct Account
{
int numberOfLines_;
double basicRate_;
double extraLineRate_;
} Account;Account newAccount(int numberOfLines, double basicRate, double extraLineRate);double computeBill(Account account);#endif /* ACCOUNT_H_ */
#include "Account.h"Account newAccount(int numberOfLines, double basicRate, double extraLineRate)
{
Account account = { 0 };
account.numberOfLines_ = numberOfLines;
account.basicRate_ = basicRate;
account.extraLineRate_ = extraLineRate; return account;
}double computeExtraLineCharges(Account account)
{
return (account.numberOfLines_ - 1) * account.extraLineRate_;
}double computeBill(Account account)
{
return account.basicRate_ + computeExtraLineCharges(account);
}
#include <embunit/embunit.h>
#include "Account.h"#define GOLD_BASIC_RATE (49.95)
#define GOLD_EXTRA_LINE_RATE (14.50)#define SILVER_BASIC_RATE (29.95)
#define SILVER_EXTRA_LINE_RATE (21.50)#define PENNY_PRECISION (0.001)Account makeGoldAccountWith(int numberOfLines)
{
return newAccount(numberOfLines, GOLD_BASIC_RATE, GOLD_EXTRA_LINE_RATE);
}Account makeSilverAccountWith(int numberOfLines)
{
return newAccount(numberOfLines, SILVER_BASIC_RATE, SILVER_EXTRA_LINE_RATE);
}
static void testGoldAccount(void)
{
ASSERT_NEAR_DBL(GOLD_BASIC_RATE, computeBill(makeGoldAccountWith(1)),
PENNY_PRECISION);
ASSERT_NEAR_DBL(GOLD_BASIC_RATE + GOLD_EXTRA_LINE_RATE,
computeBill(makeGoldAccountWith(2)), PENNY_PRECISION);
}static void testSilverAccount(void)
{
ASSERT_NEAR_DBL(SILVER_BASIC_RATE, computeBill(makeSilverAccountWith(1)),
PENNY_PRECISION);
ASSERT_NEAR_DBL(SILVER_BASIC_RATE + (2 * SILVER_EXTRA_LINE_RATE),
computeBill(makeSilverAccountWith(3)), PENNY_PRECISION);
}static void setUp(void)
{
}static void tearDown(void)
{
}
TestRef AccountTest(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(testGoldAccount),
new_TestFixture(testSilverAccount),
};
EMB_UNIT_TESTCALLER(tests, setUp, tearDown, fixtures);
return (TestRef)&tests;
}
Click on the link below to download the source code for this task.
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |