| errors/ad-hoc-unit-test/Days-in-month/2/days-month-leap.c - Funktionen daysInMonth, isLeapYear og alle tilhørende tests. | Lektion 7 - slide 17 : 25 Program 5 |
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int isLeapYear(int yr);
/* Function Under Test */
int daysInMonth(int month, int year){
int numberOfDays;
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
numberOfDays = 31; break;
case 4: case 6: case 9: case 11:
numberOfDays = 30; break;
case 2:
if (isLeapYear(year)) numberOfDays = 29;
else numberOfDays = 28; break;
default: exit(-1); break;
}
return numberOfDays;
}
/* Test functions of daysInMonth: */
void testDaysInMonthJan(void){
int actual = daysInMonth(1, 2010);
int expected = 31;
assert(actual == expected);
}
void testDaysInMonthFeb(void){
int actual = daysInMonth(2, 2010);
int expected = 28;
assert(actual == expected);
}
void testDaysInMonthApr(void){
int actual = daysInMonth(4, 2010);
int expected = 30;
assert(actual == expected);
}
void testDaysInMonthDec(void){
int actual = daysInMonth(12, 2010);
int expected = 31;
assert(actual == expected);
}
int isLeapYear(int year){
int res;
if (year % 400 == 0)
res = 1;
else if (year % 100 == 0)
res = 0;
else if (year % 4 == 0)
res = 1;
else res = 0;
return res;
}
/* A number of test functions of isLeapYear */
void testIsLeapYear1999(void){
int actual = isLeapYear(1999);
int expected = 0;
assert(actual == expected);
}
void testIsLeapYear2000(void){
int actual = isLeapYear(2000);
int expected = 1;
assert(actual == expected);
}
void testIsLeapYear1900(void){
int actual = isLeapYear(1900);
int expected = 0;
assert(actual == expected);
}
void testIsLeapYear2008(void){
int actual = isLeapYear(2008);
int expected = 1;
assert(actual == expected);
}