From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Softwaretechnik2/mixer/TestCase.cpp | 206 +++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 Bachelor/Softwaretechnik2/mixer/TestCase.cpp (limited to 'Bachelor/Softwaretechnik2/mixer/TestCase.cpp') diff --git a/Bachelor/Softwaretechnik2/mixer/TestCase.cpp b/Bachelor/Softwaretechnik2/mixer/TestCase.cpp new file mode 100644 index 0000000..851d660 --- /dev/null +++ b/Bachelor/Softwaretechnik2/mixer/TestCase.cpp @@ -0,0 +1,206 @@ +#include +#include +using namespace std; +#include // für formatierte Ausgabe im C-Stil +#define BUFLEN 160 // für Ausgabe + +#include "TestCase.h" + +// Konstruktor +TestCase::TestCase( char* name ) + : testsRun(0), testsFailed(0), testsOK(0) +{ + if (name != 0) + strncpy( tcName, name, sizeof(tcName)-1 ); + else + tcName[0] = 0; // leerer String +} + +// Hinzufügen von Testfunktionen +void TestCase::addTest( TESTFUNC f ) +{ + vecFkt.push_back( f ); +} + +// Test-Run für Test Case +void TestCase::run() +{ + testsRun = 0; // Zähler initialisieren + testsFailed = 0; + testsOK = 0; + + // Setup für Test + setUp(); + + for (int i = 0; i < vecFkt.size(); i++) + { + tL = 0; // falls nicht gesetzt durch Fkt + tN = ""; // falls nicht gesetzt durch Fkt + + bool success = true; // init + + try { + testsRun++; // Tests mitzählen + (this->*vecFkt[i])(); // Testfunktion aufrufen + } + catch ( string msg ) // Fehlerbehandlung + { + success = false; // Fehler Markieren + cout << "Fehler " << tcName << '.' << tN + << " Zeile " << tL << " " + << msg << endl; + } + + // Auswertung des Tests + if (success) + { + testsOK++; + cout << "OK: " << tcName << '.' << tN << endl; + } + else + { + testsFailed++; + } + + } // end for + + // Tear Down für Test + tearDown(); + + cout << ">>> " << tcName << ":Tests durchgefuehrt: " << testsRun + << ", o.k: " << testsOK + << ", fehlgeschlagen: " << testsFailed + << endl; +} + +void TestCase::assertEquals( int a, int b ) +{ + if (a != b) + { + char buf[BUFLEN]; + sprintf( buf, "%s: soll: %d, ist: %d", + "assertEquals(int, int)", a, b); + throw ( *(new string( buf )) ); + } +} + +void TestCase::assertEquals( long a, long b) +{ + if (a != b) + { + char buf[BUFLEN]; + sprintf( buf, "%s: soll: %d, ist: %d", + "assertEquals(long,long)", a, b); + throw ( *(new string( buf )) ); + } +} + +void TestCase::assertEquals( float a, float b) +{ + if (a != b) + { + char buf[BUFLEN]; + sprintf( buf, "%s: soll: %f, ist: %f", + "assertEquals(float,float)", a, b); + throw ( *(new string( buf )) ); + } +} + +void TestCase::assertEquals( double a, double b ) +{ + if (a != b) + { + char buf[BUFLEN]; + sprintf( buf, "%s: soll: %f, ist: %f", + "assertEquals(double,double)", a, b); + throw ( *(new string( buf )) ); + } +} + +void TestCase::assertEquals( double a, double b, double epsilon ) +{ + double diff = ( a >= b ) ? (a-b) : (b-a); + if ( diff > epsilon ) + { + char buf[BUFLEN]; + sprintf( buf, "%s: soll: %f, ist: %f, epsilon: %e", + "assertEquals(double,double,double)", a, b, epsilon); + throw ( *(new string( buf )) ); + } +} + + +void TestCase::assertEquals( bool a, bool b ) +{ + if (a != b) + { + char buf[BUFLEN]; + sprintf( buf, "%s: soll: %s, ist: %s", + "assertEquals(bool,bool)", + ((a==true) ? "true" : "false"), ((b==true) ? "true" : "false")); + throw ( *(new string( buf )) ); + } +} + +void TestCase::assertEquals( void* a, void* b) +{ + if (a != b) + { + char buf[BUFLEN]; + sprintf( buf, "%s: soll: %x, ist: %x", + "assertEquals(void*,void*)", a, b ); + throw ( *(new string( buf )) ); + } +} + +void TestCase::assertEquals( char* a, char* b ) +{ + if ( strcmp( a, b ) != 0 ) + { + char buf[BUFLEN]; // Achtung Länge hier kritisch! + const int StrMaxLen = 48; //!< Ausgabe Einzelstring begrenzen + string sa( a ); // Zuweisung auf Klasse string + string sb( b ); // erleichtert Manipulationen + if (sa.length() > StrMaxLen) + sa = sa.substr( 0, StrMaxLen ); + if (sb.length() > StrMaxLen) + sb = sb.substr( 0, StrMaxLen ); + + sprintf( buf, "%s: soll: %s, ist: %s", + "assertEquals(char*,char*)", sa.c_str(), sb.c_str() ); + throw ( *(new string( buf )) ); + } +} + + +void TestCase::assertTrue( bool a ) +{ + if (!a) + { + char buf[BUFLEN]; + sprintf( buf, "%s: soll: %s", + "assertTrue(bool,bool)", + ((a==true) ? "true" : "false") ); + throw ( *(new string( buf )) ); + } +} + +void TestCase::assertFalse( bool a) +{ + if (a) + { + char buf[BUFLEN]; + sprintf( buf, "%s: soll: %s", + "assertFalse(bool,bool)", + ((a==true) ? "true" : "false") ); + throw ( *(new string( buf )) ); + } +} + +void TestCase::assertMessage( char* msg ) +{ + char buf[BUFLEN]; + sprintf( buf, "%s: Meldung: %s", + "assertMessage( char* )", msg ); + throw ( *(new string( buf )) ); +} -- cgit v1.2.3