Initial commit

This commit is contained in:
github-classroom[bot] 2024-11-13 21:02:29 +00:00 committed by GitHub
commit 6451266878
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 2532 additions and 0 deletions

85
.gitignore vendored Normal file
View file

@ -0,0 +1,85 @@
# specific name of executable generated
car
test_car
# Created by https://www.gitignore.io/api/c++,macos,linux
# Edit at https://www.gitignore.io/?templates=c++,macos,linux
### C++ ###
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
### Linux ###
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# End of https://www.gitignore.io/api/c++,macos,linux

1624
Doxyfile Normal file

File diff suppressed because it is too large Load diff

36
README.md Normal file
View file

@ -0,0 +1,36 @@
## IF.03.22 POSE - Procedural Programming
# Assignment: Car - Abstract Data Type
[Deutsche Version](./README_de.md)
An abstract data type __Car__ should be implemented, which realizes basic functionalities of a car.
## Example
The code sequence to retrieve a car from a parking spot and accelerate it to 100 km/h would look as follows:
```c
Car c = get_car(JEEP);
if (get_color(c) == BLACK) {
set_acceleration_rate(c, 3);
while (get_speed(c) < 100)
accelerate(c);
} else {
printf("I only drive black cars!");
}
```
## Tasks
+ Define two enums, `CarType` and `Color`.
+ Create a forward declaration for the Car type so that the prototypes in the header file can be accepted by the compiler.
+ Create minimal stub implementations for all required functions. This should allow the overall project to build successfully.
+ Define the struct from the forward declaration.
+ Create local (file-specific in car.cpp only) variables for all required cars.
+ Create a local array `car_park` to store all cars.
+ Write a program in the file car_main_driver.c that races all cars defined in `car.cpp` against each other.
+ Level 1: Retrieve all cars from `car.cpp` and display them sequentially in the terminal. Represent the cars by their initial letter and a running number.
+ Level 2: Now let the cars "race" by setting each car's acceleration randomly in each round and, based on the resulting speed, move each car accordingly to the right. The race should stop as soon as one car reaches the finish line (the right edge of the screen).
>Note: Speed is given in km/h, while acceleration is given in m/s.

38
README_de.md Normal file
View file

@ -0,0 +1,38 @@
## IF.03.22 POSE - Procedural Programming
# Aufgabe: Car - Abstract Data Type
[English version](./README.md)
Es soll ein abstrakter Datentyp __Car__ implementiert werden, welcher einfache Grundfunktionalitäten eines Autos realisiert.
## Beispiel
Die Codesequenz um ein Auto aus einem Autoabstellplatz zu bekommen und auf 100 km/h zu beschleunigen wäre folgendermaßen:
```c
Car c = get_char(JEEP);
if (get_color(c) == BLACK) {
set_acceleration_rate(c, 3);
while (get_speed(c) < 100)
accelerate(c);
} else {
printf("I only drive black cars!")
}
```
## Aufgabe
- Definieren Sie zwei enums `CarType` und `Color`.
- Machen Sie eine forward-Deklaration für den Car, damit die Prototypen im h-file vom Compiler akzeptiert werden.
- Erstellen Sie minimale Leerimplementierungen für alle erforderlichen Funktionen. Damit müsste das Gesamtprojekt bauen.
- Definieren Sie das struct aus der forward-Deklaration.
- Erstellen Sie lokale (nur im File car.cpp) Variablen für alle erforderlichen Autos.
- Erstellen Sie ein lokales Array `car_park` in welchem alle Autos abgespeichert werden können.
- Schreiben Sie ein Programm in Datei `car_main_driver.c`, dass alle Autos, welche in `car.cpp` definiert sind, um die Wette fahren lässt.
- Ausbaustufe 1: Holen Sie sich alle Autos aus `car.cpp` und geben Sie diese untereinander im Terminal aus.
Symbolisieren Sie die Autos durch deren Anfangsbuchstaben und eine laufende Nummer.
- Ausbaustufe 2: Lassen Sie die Autos nun "fahren", indem Sie in jeder Runde die Beschleunigung zufällig für jedes Auto setzen und aufgrund der daraus resultierenden Geschwindigkeit das Auto entsprechend nach rechts setzen.
Das Rennen soll abgebrochen werden, sobald ein Auto das Ziel (den rechten Bildschirmrand) erreicht hat.
> Achtung: Die Geschwindingkeit wird in km/h, die Beschleunigung in m/s angegeben!

9
car.c Normal file
View file

@ -0,0 +1,9 @@
*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Author: */ <my name>; /*
* ----------------------------------------------------------
* Description:
* Implementation of car.h.
* ----------------------------------------------------------
*/

10
car.h Normal file
View file

@ -0,0 +1,10 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Author: */ <my name>; /*
* ----------------------------------------------------------
* Description:
* Car abstract data type demo.
* ----------------------------------------------------------
*/

16
car_main_driver.c Normal file
View file

@ -0,0 +1,16 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Author: */ <my name>; /*
* ----------------------------------------------------------
* Description:
* Car demo project.
* ----------------------------------------------------------
*/
#include<stdio.h>
int main(int argc, char const *argv[]) {
/* your code */
return 0;
}

45
car_test_driver.c Normal file
View file

@ -0,0 +1,45 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ----------------------------------------------------------
* Description:
* Car demo project.
* ----------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "car.h"
#include "shortcut.h"
#include "test_car.h"
int main(int argc, char *argv[])
{
ADD_TEST(get_first_aixam);
ADD_TEST(get_first_multipla);
ADD_TEST(get_second_multipla);
ADD_TEST(get_third_multipla);
ADD_TEST(get_first_jeep);
ADD_TEST(get_second_jeep);
ADD_TEST(get_car_fails);
ADD_TEST(init);
ADD_TEST(init_resets_acceleration_rate);
ADD_TEST(acceleration_rate);
ADD_TEST(too_high_acceleration_rate_for_aixam);
ADD_TEST(too_high_acceleration_rate_for_multipla);
ADD_TEST(too_high_acceleration_rate_for_jeep);
ADD_TEST(too_low_acceleration_rate_for_all_car_types);
ADD_TEST(test_accelerate);
ADD_TEST(init_resets_speed);
ADD_TEST(accelerate_aixam_to_max_speed);
ADD_TEST(accelerate_multipla_to_max_speed);
ADD_TEST(accelerate_jeep_to_max_speed);
run_tests();
return 0;
}

98
makefile Normal file
View file

@ -0,0 +1,98 @@
CC = gcc
CCLINK = g++
LIBS =
CCOPTIONS = -Wall -pedantic -std=c99 -g
LDOPTIONS =
BUILD_DIR = build
TEST = test_car
PROGRAM = car
COMMON_HDRS = general.h
LIBRARY_FILES = shortcut
ASSIGNMENT_HDRS =
ASSIGNMENT_FILES = car
TEST_FILES = test_car
MAIN_DRIVER = car_main_driver
TEST_DRIVER = car_test_driver
LIBRARY_H = $(addsuffix .h, $(LIBRARY_FILES))
ASSIGNMENT_H = $(addsuffix .h, $(ASSIGNMENT_FILES)) $(ASSIGNMENT_HDRS)
ASSIGNMENT_C = $(addsuffix .c, $(ASSIGNMENT_FILES)) $(MAIN_DRIVER).c
HDRS = $(ASSIGNEMT_H) $(SHARED_HDRS) $(COMMON_HDRS) $(LIBRARY_H)
TESTOBJECT = $(addprefix $(BUILD_DIR)/, $(TEST_DRIVER).o)
MAINOBJECT = $(addprefix $(BUILD_DIR)/, $(MAIN_DRIVER).o)
LIBRARY_OBJS = $(addprefix $(BUILD_DIR)/, $(addsuffix .o, $(LIBRARY_FILES)))
TEST_OBJS = $(addprefix $(BUILD_DIR)/, $(addsuffix .o, $(TEST_FILES)))
MAIN_OBJ = $(addprefix $(BUILD_DIR)/, $(addsuffix .o, $(ASSIGNMENT_FILES)))
OBJS = $(LIBRARY_OBJS) $(MAIN_OBJ) $(TEST_OBJS)
DOXY = doxygen
all: $(PROGRAM)
./$(PROGRAM)
$(TEST): $(BUILD_DIR) $(OBJS) $(TESTOBJECT)
$(CCLINK) -o $@ $(LDOPTIONS) $(OBJS) $(TESTOBJECT)
$(PROGRAM): $(BUILD_DIR) $(OBJS) $(MAINOBJECT)
$(CCLINK) -o $@ $(LDOPTIONS) $(OBJS) $(MAINOBJECT)
.PHONY: clean cleanall doxy test setsample setassignment definesample defineassignment assignmentfolder
clean:
rm -f $(PROGRAM) $(TEST) $(TESTOBJECT) $(MAINOBJECT) $(OBJS)
rm -rf $(BUILD_DIR)
rm -f *.o
cleanall: clean
rm -f index.html
rm -rf html
doxy:
$(DOXY)
rm -f index.html
ln -s html/index.html index.html
test: $(TEST)
./$(TEST)
cleantest: clean
clear
make test
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
$(BUILD_DIR)/%.o: %.c
$(CC) $(CCOPTIONS) -c -o $@ $<
#sets project as sample solution
setsample:
$(foreach name, $(ASSIGNMENT_H) $(ASSIGNMENT_C), cp $(name).sample $(name);)
#sets project as assignment
setassignment:
$(foreach name, $(ASSIGNMENT_H) $(ASSIGNMENT_C), cp $(name).assignment $(name);)
# defines current state of project as sample solution
definesample:
$(foreach name, $(ASSIGNMENT_H) $(ASSIGNMENT_C), cp $(name) $(name).sample;)
# defines current sate of project as assignment
defineassignment :
$(foreach name, $(ASSIGNMENT_H) $(ASSIGNMENT_C), cp $(name) $(name).assignment;)
# creates a folder which can serve as a publishable assignment
assignmentfolder:
make setassignment
make doxy
rm -rf ../assignment
mkdir ../assignment
cp -R * ../assignment
cp .gitignore ../assignment
rm ../assignment/*.sample
rm ../assignment/*.assignment
make cleanall

151
shortcut.c Normal file
View file

@ -0,0 +1,151 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Author: P. Bauer
* Date: November 08, 2010
* ----------------------------------------------------------
* Description:
* Test driver.
* ----------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include "shortcut.h"
#define MAX_TEST_FUNCTIONS 256
static char assert_msg_buffer[1024];
static int tc_count = 0;
static int tc_fail_count = 0;
static struct TestCase test_cases[MAX_TEST_FUNCTIONS];
const char* version()
{
return "ShortCut v. 1.3.0";
}
char* format_msg(char* format, ...) {
va_list args;
va_start (args, format);
vsprintf(assert_msg_buffer, format, args);
return assert_msg_buffer;
}
void assert_true(bool bool_expr, struct TestCase *tc, const char *msg,
const char* file, int line)
{
if (!bool_expr) {
if (tc->success) {
tc->success = false;
tc_fail_count++;
}
printf("\n\tFailure (file: %s, line %d): %s: %s", file, line, tc->name, msg);
}
}
void assert_false(bool bool_expr, struct TestCase *tc, const char *msg,
const char* file, int line)
{
assert_true(!bool_expr, tc, msg, file, line);
}
static void assert_string_failure(const char *expected, char *actual, struct TestCase *tc,
const char *msg, const char* file, int line);
void assert_equals_str(const char *expected, char *actual, struct TestCase *tc,
const char *msg, const char* file, int line)
{
if (expected == actual) {
return;
}
if (expected == 0 || actual == 0) {
assert_string_failure(expected, actual, tc, msg, file, line);
return;
}
if (strcmp(actual, expected) != 0) {
assert_string_failure(expected, actual, tc, msg, file, line);
return;
}
}
#define MAX_MSG_LEN 128
static void assert_string_failure(const char *expected, char *actual, struct TestCase *tc,
const char *msg, const char* file, int line)
{
char new_msg[MAX_MSG_LEN];
sprintf(new_msg, "Expected \"%s\", actual \"%s\". %s", expected, actual, msg);
assert_true(false, tc, new_msg, file, line);
}
void assert_equals(int expected, int actual, struct TestCase *tc,
const char *msg, const char* file, int line)
{
char new_msg[MAX_MSG_LEN];
sprintf(new_msg, "Expected %d, actual %d. %s", expected, actual, msg);
assert_true(expected == actual, tc, new_msg, file, line);
}
void assert_equals_f(double expected, double actual, double tolerance, struct TestCase* tc,
const char* msg, const char* file, int line)
{
char new_msg[MAX_MSG_LEN];
sprintf(new_msg, "Expected %f, actual %f. %s", expected, actual, msg);
double min_val = expected - tolerance;
double max_val = expected + tolerance;
assert_true(min_val <= actual && actual <= max_val, tc, new_msg, file, line);
}
int get_test_count()
{
return tc_count;
}
bool add_test(void (*test_function)(struct TestCase *tc), const char *test_name)
{
if (tc_count == MAX_TEST_FUNCTIONS) {
return false;
}
else {
test_cases[tc_count].success = true;
test_cases[tc_count].name = test_name;
test_cases[tc_count].test_function = test_function;
tc_count++;
return true;
}
}
void run_tests()
{
int i;
printf("\n%s: Running tests\n", version());
for (i = 0; i < get_test_count(); i++) {
printf("Running test %s ...", test_cases[i].name);
test_cases[i].test_function(&test_cases[i]);
if (test_cases[i].success) {
printf("\033[32m OK\033[m");
}
else {
printf("\033[31m ... FAIL\033[m");
}
printf("\n");
}
printf("\nTotal tests run: %d\n", tc_count);
if (tc_fail_count > 0) {
printf("\033[31mTests failed: %d\033[m\n", tc_fail_count);
}
else {
printf("\033[32mAll tests run successfully\033[m\n");
}
}

111
shortcut.h Normal file
View file

@ -0,0 +1,111 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Author: P. Bauer
* Date: November 03, 2010
* ----------------------------------------------------------
* Description:
* A simple unit testing frame work for C.
* ----------------------------------------------------------
*/
#ifndef ___SHORTCUT_H
#define ___SHORTCUT_H
#include <stdbool.h>
/** TestCase is the struct to define one test case. A test case can
*** be added to a test. If the test is run all added test cases are
*** run and the result of the run of each test case is checked automatically.
*/
struct TestCase {
const char *name;
/** true if the test passed, false otherwise. */
bool success;
/** The test function which is executed by the test framework. */
void (*test_function)(struct TestCase *tc);
};
/**
*** @return Version of shortcut as string
***/
const char* version();
/**
*** @return The fromated string as generated using sprintf(format, ...)
***/
char* format_msg(char* format, ...);
/** assert_true checks, whether a boolean expression passed is true or false.
*** in case it is false the test case stating the assertion is marked
*** as failed and msg is printed.
*** @param bool_expr Expression which is evaluated.
*** @param tc Pointer to the test case which states this assertion.
*** @param msg Message to be printed if assertion evaluates to false.
*** @param file File in which the assert is given.
*** @param line Line in which the assert is given.
*/
void assert_true(bool bool_expr, struct TestCase *tc, const char *msg,
const char* file, int line);
/** assert_false does the same as assert() but the boolean expression
*** has to evaluate to false. If it evaluates to true the assertion
*** fails.
*** @see assert
*/
void assert_false(bool bool_expr, struct TestCase* tc, const char* msg,
const char* file, int line);
/** assert_equals checks whether two values are equal. Currently the following
*** data formats are supported:
*** - strings
*** - integer
*** @param expected The expected string value
*** @param actual The actual string value
*** @param tc Pointer to the test case which states this assertion.
*** @param msg Message to be printed if assertion evaluates to false.
*** @param file File in which the assert is given.
*** @param line Line in which the assert is given.
*** @see assert
*/
void assert_equals(int expected, int actual, struct TestCase* tc,
const char* msg, const char* file, int line);
void assert_equals_str(const char* expected, char* actual, struct TestCase* tc,
const char* msg, const char* file, int line);
void assert_equals_f(double expected, double actual, double tolerance, struct TestCase* tc,
const char* msg, const char* file, int line);
/** @return The total number of test cases added to the test.
*/
int get_test_count();
/** add_test creates a new test case and adds the a test function to
*** this test case.
*** @param test_function Pointer to the test function to be added
*** to the newly created test case.
*** @param test_case_name Name which should be assigned to the newly
*** created test case.
*/
bool add_test(void (*test_function)(struct TestCase *tc), const char *test_case_name);
void run_tests();
#define TEST(testname) void testname(struct TestCase *tc)
#define MSG(format, ...) format_msg(format, ##__VA_ARGS__)
#define ASSERT_TRUE(condition, msg) assert_true(condition, tc, msg, __FILE__, __LINE__)
#define ASSERT_FALSE(condition, msg) assert_false(condition, tc, msg, __FILE__, __LINE__)
#define ASSERT_EQUALS(expected, actual) assert_equals(expected, actual, tc, "", __FILE__, __LINE__)
#define ASSERT_EQUALS_STR(expected, actual, msg) assert_equals_str(expected, actual, tc, msg, __FILE__, __LINE__)
#define ASSERT_EQUALS_TOLERANCE(expected, actual, tolerance) assert_equals_f(expected, actual, tolerance, tc, "", __FILE__, __LINE__)
#define ASSERT_EQUALS_TOLERANCE_STR(expected, actual, tolerance, msg) assert_equals_f(expected, actual, tolerance, tc, msg, __FILE__, __LINE__)
#define ADD_TEST(testfunction) add_test(testfunction, #testfunction)
#endif

274
test_car.c Normal file
View file

@ -0,0 +1,274 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Description:
* Test functions for Car.
* ----------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "shortcut.h"
#include "car.h"
#include "test_car.h"
/**
* get_first_aixam tests the properties of the first aixam in the car park
*/
TEST(get_first_aixam)
{
Car car1 = get_car(AIXAM);
ASSERT_FALSE(car1 == 0, "Must get the first Axiam");
ASSERT_EQUALS(AIXAM, get_type(car1));
ASSERT_EQUALS(RED, get_color(car1));
ASSERT_EQUALS(16.0, get_fill_level(car1));
ASSERT_EQUALS(0.0, get_acceleration_rate(car1));
ASSERT_EQUALS(0, get_speed(car1));
}
/**
* your description:
*/
TEST(get_first_multipla)
{
Car car1 = get_car(FIAT_MULTIPLA);
ASSERT_FALSE(car1 == 0, "Must get the first Multipla");
ASSERT_EQUALS(FIAT_MULTIPLA, get_type(car1));
ASSERT_EQUALS(GREEN, get_color(car1));
ASSERT_EQUALS(65.0, get_fill_level(car1));
ASSERT_EQUALS(0.0, get_acceleration_rate(car1));
ASSERT_EQUALS(0, get_speed(car1));
}
/**
* your description:
*/
TEST(get_second_multipla)
{
Car car1 = get_car(FIAT_MULTIPLA);
ASSERT_FALSE(car1 == 0, "Must get the second Multipla");
ASSERT_EQUALS(FIAT_MULTIPLA, get_type(car1));
ASSERT_EQUALS(BLUE, get_color(car1));
ASSERT_EQUALS(65.0, get_fill_level(car1));
ASSERT_EQUALS(0.0, get_acceleration_rate(car1));
ASSERT_EQUALS(0, get_speed(car1));
}
/**
* your description:
*/
TEST(get_third_multipla)
{
Car car1 = get_car(FIAT_MULTIPLA);
ASSERT_FALSE(car1 == 0, "Must get the third Multipla");
ASSERT_EQUALS(FIAT_MULTIPLA, get_type(car1));
ASSERT_EQUALS(ORANGE, get_color(car1));
ASSERT_EQUALS(65.0, get_fill_level(car1));
ASSERT_EQUALS(0.0, get_acceleration_rate(car1));
ASSERT_EQUALS(0, get_speed(car1));
}
/**
* your description:
*/
TEST(get_first_jeep)
{
Car car1 = get_car(JEEP);
ASSERT_FALSE(car1 == 0, "Must get the first Jeep");
ASSERT_EQUALS(JEEP, get_type(car1));
ASSERT_EQUALS(SILVER, get_color(car1));
ASSERT_EQUALS(80.0, get_fill_level(car1));
ASSERT_EQUALS(0.0, get_acceleration_rate(car1));
ASSERT_EQUALS(0, get_speed(car1));
}
/**
* your description:
*/
TEST(get_second_jeep)
{
Car car1 = get_car(JEEP);
ASSERT_FALSE(car1 == 0, "Must get the second Jeep");
ASSERT_EQUALS(JEEP, get_type(car1));
ASSERT_EQUALS(BLACK, get_color(car1));
ASSERT_EQUALS(80.0, get_fill_level(car1));
ASSERT_EQUALS(0.0, get_acceleration_rate(car1));
ASSERT_EQUALS(0, get_speed(car1));
}
/**
* your description:
*/
TEST(get_car_fails)
{
ASSERT_TRUE(get_car(AIXAM) == 0, "No second axiam available");
ASSERT_TRUE(get_car(FIAT_MULTIPLA) == 0, "No fourth Multi available");
ASSERT_TRUE(get_car(JEEP) == 0, "No third jeep available");
}
/**
* Tests whether init sets all cars in car park back to be
* available.
*/
TEST(test_init) {
init();
ASSERT_TRUE(get_car(AIXAM) != 0, "One Axiam available");
ASSERT_TRUE(get_car(AIXAM) == 0, "Only one Axiam available");
ASSERT_TRUE(get_car(FIAT_MULTIPLA) != 0, "First Multi available");
ASSERT_TRUE(get_car(FIAT_MULTIPLA) != 0, "Second Multi available");
ASSERT_TRUE(get_car(FIAT_MULTIPLA) != 0, "Third Multi available");
ASSERT_TRUE(get_car(FIAT_MULTIPLA) == 0, "Only three Multis available");
ASSERT_TRUE(get_car(JEEP) != 0, "First Jeep available");
ASSERT_TRUE(get_car(JEEP) != 0, "Second Jeep available");
ASSERT_TRUE(get_car(JEEP) == 0, "Only two Jeeps available");
}
/**
* your description:
*/
TEST(init_resets_acceleration_rate)
{
init();
Car car = get_car(AIXAM);
ASSERT_EQUALS(0, get_acceleration_rate(car));
set_acceleration_rate(car, 1);
ASSERT_EQUALS(1, get_acceleration_rate(car));
init();
ASSERT_EQUALS(0, get_acceleration_rate(get_car(AIXAM)));
}
/**
* your description:
*/
TEST(acceleration_rate)
{
init();
Car car = get_car(JEEP);
set_acceleration_rate(car, 3.14);
ASSERT_EQUALS_TOLERANCE(3.14, get_acceleration_rate(car), 0.1);
}
/**
* your description:
*/
TEST(too_high_acceleration_rate_for_aixam)
{
init();
Car car = get_car(AIXAM);
set_acceleration_rate(car, 1.01);
ASSERT_EQUALS_TOLERANCE(1.0, get_acceleration_rate(car), 0.00001);
}
/**
* your description:
*/
TEST(too_high_acceleration_rate_for_multipla)
{
init();
Car car = get_car(FIAT_MULTIPLA);
set_acceleration_rate(car, 2.27);
ASSERT_EQUALS_TOLERANCE(2.26, get_acceleration_rate(car), 0.00001);
}
/**
* your description:
*/
TEST(too_high_acceleration_rate_for_jeep)
{
init();
Car car = get_car(JEEP);
set_acceleration_rate(car, 3.15);
ASSERT_EQUALS_TOLERANCE(3.14, get_acceleration_rate(car), 0.00001);
}
/**
* your description:
*/
TEST(too_low_acceleration_rate_for_all_car_types)
{
init();
Car aixam = get_car(AIXAM);
set_acceleration_rate(aixam, -8.0);
ASSERT_EQUALS_TOLERANCE(-8.0, get_acceleration_rate(aixam), 0.001);
set_acceleration_rate(aixam, -8.01);
ASSERT_EQUALS_TOLERANCE(-8.0, get_acceleration_rate(aixam), 0.001);
}
/******************************************************************************
* test_accelerate
*
* tests acceleration of car without any limits.
******************************************************************************/
TEST(test_accelerate)
{
init();
Car car = get_car(AIXAM);
ASSERT_EQUALS(0, get_speed(car));
set_acceleration_rate(car, 1.0);
accelerate(car);
ASSERT_EQUALS(4, get_speed(car));
}
/**
* your description:
*/
TEST(init_resets_speed)
{
init();
Car car = get_car(AIXAM);
ASSERT_EQUALS(0, get_speed(car));
set_acceleration_rate(car, 0.5);
accelerate(car);
ASSERT_EQUALS(2, get_speed(car));
init();
ASSERT_EQUALS(0, get_speed(get_car(AIXAM)));
}
/**
* your description:
*/
TEST(accelerate_aixam_to_max_speed)
{
init();
Car car = get_car(AIXAM);
ASSERT_EQUALS(0, get_speed(car));
set_acceleration_rate(car, 1);
for (int i = 0; i < 12; i++)
accelerate(car);
ASSERT_EQUALS(43, get_speed(car));
accelerate(car);
ASSERT_EQUALS(45, get_speed(car));
}
/**
* your description:
*/
TEST(accelerate_multipla_to_max_speed)
{
init();
Car car = get_car(FIAT_MULTIPLA);
ASSERT_EQUALS(0, get_speed(car));
set_acceleration_rate(car, 2.26);
for (int i = 0; i < 20; i++)
accelerate(car);
ASSERT_EQUALS(163, get_speed(car));
accelerate(car);
ASSERT_EQUALS(170, get_speed(car));
}
/**
* your description:
*/
TEST(accelerate_jeep_to_max_speed)
{
init();
Car car = get_car(JEEP);
ASSERT_EQUALS(0, get_speed(car));
set_acceleration_rate(car, 3.14);
for (int i = 0; i < 17; i++)
accelerate(car);
ASSERT_EQUALS(192, get_speed(car));
accelerate(car);
ASSERT_EQUALS(196, get_speed(car));
}

35
test_car.h Normal file
View file

@ -0,0 +1,35 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Description:
* Test functions for car.
* ----------------------------------------------------------
*/
#ifndef ___TEST_CAR_H
#define ___TEST_CAR_H
#include "shortcut.h"
TEST(get_first_aixam);
TEST(get_first_multipla);
TEST(get_second_multipla);
TEST(get_third_multipla);
TEST(get_first_jeep);
TEST(get_second_jeep);
TEST(test_init);
TEST(init_resets_acceleration_rate);
TEST(get_car_fails);
TEST(acceleration_rate);
TEST(too_high_acceleration_rate_for_aixam);
TEST(too_high_acceleration_rate_for_multipla);
TEST(too_high_acceleration_rate_for_jeep);
TEST(too_low_acceleration_rate_for_all_car_types);
TEST(test_accelerate);
TEST(init_resets_speed);
TEST(accelerate_aixam_to_max_speed);
TEST(accelerate_multipla_to_max_speed);
TEST(accelerate_jeep_to_max_speed);
#endif