Last active
September 2, 2015 18:33
-
-
Save sjwiesman/e93aab81b26e61c32ac0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <assert.h> | |
typedef struct _car { | |
char model[20]; | |
char brand[20]; | |
int year; | |
long worth; | |
} Car; | |
typedef struct _house{ | |
int sqFootage; | |
long worth; | |
} House; | |
typedef struct _person { | |
char firstname[20]; | |
char lastname[20]; | |
int numCars; | |
Car ownedCars[2]; | |
House ownedHouse; | |
} Person; | |
Person readPeople(FILE* fp); | |
int calculateTotal(Person people); | |
Car findMostExpensiveCar(Person* people); | |
int main(int argc, char** argv) { | |
//Create a person to test against | |
Person bobSmith = { | |
"Bob", | |
"Smith", | |
1, | |
{{ | |
"Ford", | |
"Mustang", | |
1964, | |
20000 | |
}}, | |
{ | |
50000, | |
1000000 | |
} | |
}; | |
Person joeSmith = { | |
"Joe", | |
"Smith", | |
2, | |
{{ | |
"Honda", | |
"Civic", | |
2009, | |
7000, | |
},{ | |
"Jeep", | |
"Grand Cheroke", | |
2011, | |
10000 | |
}}, | |
{ | |
1000, | |
300000 | |
} | |
}; | |
Person people[2] = [bobSmith, joeSmith]; | |
//Generate a sample input | |
FILE* fp = fopen("sampleInput.txt", "w"); | |
if (fp == NULL) { | |
return (EXIT_FAILURE); | |
} | |
fprintf("%s %s\n%d\n%d %s %s %ld\n%d %ld", | |
bobSmith.firstname, | |
bobSmith.lastname, | |
bobSmith.numCars, | |
bobSmith.ownedCars[0].year, | |
bobSmith.ownedCars[0].brand, | |
bobSmith.ownedCars[0].model, | |
bobSmith.ownedCars[0].worth, | |
bobSmith.ownedHouse.sqFootage, | |
bobSmith.ownedHouse.worth | |
); | |
fprintf("%s %s\n%d\n%d %s %s %ld\n%d %s %s %ld\n%d %ld", | |
joeSmith.firstname, | |
joeSmith.lastname, | |
joeSmith.numCars, | |
joeSmith.ownedCars[0].year, | |
joeSmith.ownedCars[0].brand, | |
joeSmith.ownedCars[0].model, | |
joeSmith.ownedCars[0].worth, | |
joeSmith.ownedCars[1].year, | |
joeSmith.ownedCars[1].brand, | |
joeSmith.ownedCars[1].model, | |
joeSmith.ownedCars[1].worth, | |
joeSmith.ownedHouse.sqFootage, | |
joeSmith.ownedHouse.worth | |
); | |
fclose(fp); | |
printf("Beginning readPeople test"); | |
fp = fopen("sampleInput.txt", "r"); | |
const Person person1 = readPeople(fp); | |
assert( | |
strcmp(person1.firstname,bobSmith.firstname) == 0 | |
); | |
assert( | |
strcmp(person1.lastname, bobSmith.lastname) == 0 | |
); | |
assert( | |
person1.numCars == bobSmith.numCars | |
); | |
assert( | |
strcmp(person1.ownedCars[0].brand, bobSmith.ownedCars[0].brand) == 0 | |
); | |
assert( | |
strcmp(person1.ownedCars[0].model, bobSmith.ownedCars[0].model) == 0 | |
); | |
assert( | |
person1.ownedCars[0].worth == bobSmith.ownedCars[0].worth | |
); | |
assert( | |
person1.ownedCars[0].year == bobSmith.ownedCars[0].year | |
); | |
assert( | |
person1.ownedHouse.sqFootage == bobSmith.ownedHouse.sqFootage | |
); | |
assert( | |
person1.ownedHouse.worth == bobSmith.ownedHouse.worth | |
); | |
const Person person2 = readPeople(fp); | |
assert( | |
strcmp(person2.firstname, joeSmith.firstname) == 0 | |
); | |
assert( | |
strcmp(person2.lastname, joeSmith.lastname) == 0 | |
); | |
assert( | |
person2.numCars == joeSmith.numCars | |
); | |
assert( | |
strcmp(person2.ownedCars[0].brand, joeSmith.ownedCars[0].brand) == 0 | |
); | |
assert( | |
strcmp(person2.ownedCars[0].model, joeSmith.ownedCars[0].model) == 0 | |
); | |
assert( | |
person2.ownedCars[0].worth == joeSmith.ownedCars[0].worth | |
); | |
assert( | |
person2.ownedCars[0].year == joeSmith.ownedCars[0].year | |
); | |
assert( | |
strcmp(person2.ownedCars[1].brand, joeSmith.ownedCars[1].brand) == 0 | |
); | |
assert( | |
strcmp(person2.ownedCars[1].model, joeSmith.ownedCars[1].model) == 0 | |
); | |
assert( | |
person2.ownedCars[1].worth == joeSmith.ownedCars[1].worth | |
); | |
assert( | |
person2.ownedCars[1].year == joeSmith.ownedCars[1].year | |
); | |
assert( | |
person2.ownedHouse.sqFootage == joeSmith.ownedHouse.sqFootage | |
); | |
assert( | |
person2.ownedHouse.worth == joeSmith.ownedHouse.worth | |
); | |
fclose(fp); | |
remove("sampleInput.txt"); | |
printf("Passed all readPeople tests"); | |
printf("Beginning calculateTotal tests"); | |
const int bobNetWorth = calculateTotal(bobSmith); | |
assert(bobNetWorth == 1020000); | |
const int joeNetWorth = calculateTotal(joeNetWorth); | |
assert(joeNetWorth == 317000); | |
printf("Passed all calculateTotal tests"); | |
printf("Beginning findMostExpensiveCar tests"); | |
const Car bobsMostExpensiveCar = findMostExpensiveCar(bobSmith); | |
assert( | |
strcmp(bobsMostExpensiveCar.brand, bobSmith.ownedCars[0].brand) == 0 | |
); | |
const Car mostExpensiveCar = findMostExpensiveCar(people); | |
assert( | |
strcmp(mostExpensiveCar.brand, joeSmith.ownedCars[0].brand) == 0 | |
); | |
printf("Passed all findMostExpensiveCar tests"); | |
printf("Congratulations, delete this code and implement main"); | |
return (EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment