summaryrefslogtreecommitdiffstats
path: root/Bachelor/Softwaretechnik2/code/TestCase.cpp
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Bachelor/Softwaretechnik2/code/TestCase.cpp
downloadStudium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz
Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Bachelor/Softwaretechnik2/code/TestCase.cpp')
-rw-r--r--Bachelor/Softwaretechnik2/code/TestCase.cpp206
1 files changed, 206 insertions, 0 deletions
diff --git a/Bachelor/Softwaretechnik2/code/TestCase.cpp b/Bachelor/Softwaretechnik2/code/TestCase.cpp
new file mode 100644
index 0000000..851d660
--- /dev/null
+++ b/Bachelor/Softwaretechnik2/code/TestCase.cpp
@@ -0,0 +1,206 @@
+#include <iostream>
+#include <string>
+using namespace std;
+#include <stdio.h> // 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 )) );
+}