Probleme mit Testtreiber



  • Hallo,

    ich muss für mein Praktikum ein Testprogramm schreiben was alle fälle testet und anschließend mMldung wieder gibt ob Test bestanden oder nicht. Im Hauptprogramm geht es um Delegation. Das Programm soll einfach verschiedene Datumsformate wiedergeben.

    Habe für die meisten Funktionen die Fehlersuche erfolgreich erstellt aber bei einer Funktion wo die Differenz von 2 Datumen Angezeigt werden soll.

    Quellcode ( Die Diff Funktion ist auskommentiert, weil die nicht funktioniert)

    #include <iostream>
    #include <string>
    #include "exception.h"
    #include "Date.h"
    
    using namespace std;
    
    bool isLeapYeartest();
    bool getDayInYeartest();
    bool getDayInWeektest();
    bool getWeekInYeartest();
    //bool difftest(Date d);
    bool toStringtest();
    
    int main(void)
    {
    
    	bool ok = true;
    	string temp;
    
    	if (ok) {
    		if ( true == (ok = isLeapYeartest()))
    			cout << "Schaltjahr Test bestanden \n";
    		else cout << "Schaltjahr Test fehlgeschlagen\n";
    	}
    
    		if (ok) {
    		if ( true == (ok = getDayInYeartest()))
    			cout << "getDayInYear Test bestanden \n";
    		else cout << "getDayInYear Test fehlgeschlagen\n";
    	}
    
    		if (ok) {
    		if ( true == (ok = getDayInWeektest()))
    			cout << "getDayInWeek Test bestanden \n";
    		else cout << "getDayInWeek Test fehlgeschlagen\n";
    	}
    
    		if (ok) {
    		if ( true == (ok = getWeekInYeartest()))
    			cout << "getWeekInYear Test bestanden \n";
    		else cout << "getWeekInYear Test fehlgeschlagen\n";
    	}
    
    	/*	if (ok) {
    		if ( true == (ok = difftest(Date d)))
    			cout << "Differenz Text bestanden \n";
    		else cout << "Differenz Test fehlgeschlagen\n";
    	}*/
    
    		if (ok) {
    		if ( true == (ok = toStringtest()))
    			cout << "toString Test bestanden \n";
    		else cout << "toString Test fehlgeschlagen\n";
    	}
    
    	return 0;
    }
    
    bool isLeapYeartest()
    {
    	bool ok;
    
    	DateFormat *de = new DateFormatDE();
    	DateFormat *en = new DateFormatEN();
    
    	Date d(de,20,3,2009);
    	Date e(de,20,3,2008);
    
    	if(!d.isLeapYear()){
    		ok= true;
    	}
    	else{
    		ok=false;
    	}
    
    	if(e.isLeapYear()){
    		ok=true;
    	}
    	else{
    		ok=false;
    	}
    
    	return ok;
    
    }
    
    bool getDayInYeartest()
    {
    	bool ok;
    
    	DateFormat *de = new DateFormatDE();
    	DateFormat *en = new DateFormatEN();
    
    	Date d(de,20,3,2009);
    
    	if(d.getDayInYear()==79){
    		ok=true;
    	}
    	else{
    		ok=false;
    	}
    
    	return ok;
    }
    
    bool getDayInWeektest()
    {
    	bool ok;
    
    	DateFormat *de = new DateFormatDE();
    	DateFormat *en = new DateFormatEN();
    
    	Date d(de,4,6,2009);
    
    	if(d.getDayInWeek()==4){
    		ok=true;
    	}
    	else{
    		ok=false;
    	}
    
    	return ok;
    }
    
    bool getWeekInYeartest()
    {
    	bool ok;
    
    	DateFormat *de = new DateFormatDE();
    	DateFormat *en = new DateFormatEN();
    
    	Date d(de,11,1,2009);
    
    	if(d.getWeekInYear()==2){
    		ok=true;
    	}
    	else{
    		ok=false;
    	}
    
    	return ok;
    }
    
    //bool difftest(Date d)()
    //{
    //	bool ok;
    //
    //	DateFormat *de = new DateFormatDE();
    //	DateFormat *en = new DateFormatEN();
    //
    //	
    //	Date d(de,20,2,2009);
    //	Date e(de,19,1,2009);
    //
    //	if(d.diff(Date d)- e.diff(Date d)== 42{
    //		ok=true;
    //	}
    //	else{
    //		ok=false;
    //	}
    //
    //	return ok;
    //}
    
    bool toStringtest()
    {
    	bool ok;
    
    	DateFormat *de = new DateFormatDE();
    	DateFormat *en = new DateFormatEN();
    
    	Date d(de,1,11,2015);
    
    	if(d.toString()=="1.11.2015"){
    		ok=true;
    	}
    	else{
    		ok=false;
    	}
    
    	return ok;
    }
    

    Hauptprogramm

    #include <iostream>
    #include <string>
    #include <math.h>
    #include "Date.h"
    #include "DateFormat.h"
    #include "exception.h"
    using namespace std;
    
    /* default-Darstellung: DE */ 
    Date::Date(int day, int month, int year)
    {
    	DateFormatDE *DE = new DateFormatDE();
    	_df = DE;
    	_day = day;
    	_month = month;
    	_year = year;
    
    	if (_day < 1)
    		throw Exception::Exception("wrong day count (negative)");
    	if ((_day > 27) && (_month == 2) && (!isLeapYear())) /* kein Schaltjahr, aber Feb >27 Tage */ 
    		throw Exception::Exception("wrong day for this month and year");
    	if ((_day > 28) && (_month == 2)) /* Februar >28 Tage angegeben */ 
    		throw Exception::Exception("wrong day for this month");
    	if ((_day > 30) && ((_month == 4) /* Apr., Jun., Sept., Nov. >30 Tage angegeben*/ 
    		|| (_month == 6) || (_month == 9) || (_month == 11)))
    		throw Exception::Exception("wrong day cout for this month");
    	if (_day > 31) /* mehr als 31 Tage angegeben */ 
    		throw Exception::Exception("wrong day count");
    	if (_month < 1)
    		throw Exception::Exception("wrong month count (negative)");
    	if (_month > 12)
    		throw Exception::Exception("wrong month count");
    	if (_year < 0)
    		throw Exception::Exception("wrong year count (negative)");
    }
    /* Konstruktor */ 
    Date::Date(DateFormat * dformat, int day, int month, int year)
    {
    	_df = dformat;
    	_day = day;
    	_month = month;
    	_year = year;
    
    	if (!dformat) /* kein DateFormat angegeben */ 
    		throw Exception::Exception("wrong DateFormat");
    	if (_day < 1)
    		throw Exception::Exception("wrong day count (negative)");
    	if ((_day > 27) && (_month == 2) && (!isLeapYear())) /* kein Schaltjahr, aber Feb >27 Tage */ 
    		throw Exception::Exception("wrong day for this month and year");
    	if ((_day > 28) && (_month == 2)) /* Februar >28 Tage angegeben */ 
    		throw Exception::Exception("wrong day for this month");
    	if ((_day > 30) && ((_month == 4) /* Apr., Jun., Sept., Nov. >30 Tage angegeben*/ 
    		|| (_month == 6) || (_month == 9) || (_month == 11)))
    		throw Exception::Exception("wrong day cout for this month");
    	if (_day > 31) /* mehr als 31 Tage angegeben */ 
    		throw Exception::Exception("wrong day count");
    	if (_month < 1)
    		throw Exception::Exception("wrong month count (negative)");
    	if (_month > 12)
    		throw Exception::Exception("wrong month count");
    	if (_year < 1)
    		throw Exception::Exception("wrong year count (negative)");
    }
    
    /* public outputs */ 
    int Date::getDateDay()
    {
    	return _day;
    }
    int Date::getDateMonth()
    {
    	return _month;
    }
    int Date::getDateYear()
    {
    	return _year;
    }
    /* Abstand zweier Daten */ 
    int Date::diff(Date d)
    {
    	double erg = 0;
    	if (d._year != _year)
    		erg = (abs(d._year - _year) * 365.25);
    	return (int)erg + abs( d.getDayInYear() - getDayInYear() );
    }
    
    /* Tag des Jahres */ 
    int Date::getDayInYear()
    {
    	int tage = 0;
    	int m = 1;
    	while (m < _month)
    	{
    		if (m == 1) tage = tage + 31;
    		if (m == 2) tage = tage + 28;
    		if (m == 3) tage = tage + 31;
    		if (m == 4) tage = tage + 30;
    		if (m == 5) tage = tage + 31;
    		if (m == 6) tage = tage + 30;
    		if (m == 7) tage = tage + 31;
    		if (m == 8) tage = tage + 31;
    		if (m == 9) tage = tage + 30;
    		if (m == 10) tage = tage + 31;
    		if (m == 11) tage = tage + 30;
    		if (m == 12) tage = tage + 31;
    		m++;
    	}
    	tage = tage + _day;
    	if ((_month > 2) && isLeapYear()) tage++;
    	return tage;
    }
    
    /* Wochentag 0-6 = So-Sa */ 
    int Date::getDayInWeek()
    {
    	int dz = _day%7;
    	int mz = 0;
    	if (_month == 1) mz = 0;
    	if (_month == 2) mz = 3;
    	if (_month == 3) mz = 3;
    	if (_month == 4) mz = 6;
    	if (_month == 5) mz = 1;
    	if (_month == 6) mz = 4;
    	if (_month == 7) mz = 6;
    	if (_month == 8) mz = 2;
    	if (_month == 9) mz = 5;
    	if (_month == 10) mz = 0;
    	if (_month == 11) mz = 3;
    	if (_month == 12) mz = 5;
    	int yz = ( _year%100 + (int)((_year%100)/4) ) % 7;
    	int cz = (3 - (int) (_year/100) % 4 )*2;
    	int sz = 0;
    	if (((_month == 1) || (_month == 2)) && isLeapYear()) sz = 1;
    
    	int erg = (dz+mz+yz+cz-sz)%7;
    	//Berechnung lt. http://de.wikipedia.org/wiki/Wochentagsberechnung
    	return erg;
    }
    
    /* Kalenderwoche */ 
    int Date::getWeekInYear()
    {
    	/*
    	*	- die woche die den 4. jan enthält ist die erste KW
    	*	- wenn 4.jan Mo, Di oder Mi -> zahl
    	*	- wenn 4.jan Do, Fr, Sa, So -> zahl+1 (Do nicht mit rein, sonntag raus weil 0)
    	*/
    	Date t(04, 01, _year);
    	int jan4 = t.getDayInWeek();
    
    	int kw = (getDayInYear() - getDayInWeek()) / 7; /* Ohne /7 wird immer letzter sonntag vor datums-woche sein
    													* mit 7 gibt die vollen wochen aus, die zwischen unserem datum und 1. sonntag im jahr liegen */ 
    
    	if (jan4 == 5 || jan4 == 6 || jan4 == 0) kw++; /*
    												   * falls 4.Jan ein Fr, Sa, So ist !
    												   * wenn 1.Jan ist Montag, DANN ist 4.Jan DO
    												   * -> ist schon oben bei /7 mit drin
    												   */ 
    
    	if (getDayInWeek() != 0) kw++; /* falls wir sonntags anfangen würden */ 
    
    	//cout << "4.Jan: " << jan4 << "\t";
    	return kw;
    }
    
    /* Schaltjahr? */ 
    bool Date::isLeapYear()
    {
    	if ((_year%100 == 0 && _year%4 != 0)
    		|| (_year%4 == 0 && _year%100 != 0)
    		|| (_year%400 == 0))
    		return true;
    	else return false;
    }
    
    /* Date output */ 
    string Date::toString()
    {
    	return _df->format(*this);
    }
    
    /* format change */ 
    void Date::setFormat(DateFormat *f)
    {
    	_df = f;
    }
    
    ///* test-main */ 
    //int main(void)
    //{
    //	DateFormat *DE = new DateFormatDE();
    //	DateFormat *EN = new DateFormatEN();
    //	DateFormat *US = new DateFormatUS();
    //	int i = 4;
    //	int a = i+4;
    //	for (i; i <= a; i++)
    //	{
    //		try {
    //			Date test(i, 2, 2009);
    //			test.setFormat(EN);
    //			cout << test.toString()
    //				<< "\tKW: " << test.getWeekInYear()
    //				<< "\tTag: " << test.getDayInWeek()
    //				<< endl;
    //			test.setFormat(US);
    //			cout << test.toString()
    //				<< "\tKW: " << test.getWeekInYear()
    //				<< "\tTag: " << test.getDayInWeek()
    //				<< endl;
    //			Date test2(EN, i, i, i+1000);
    //			test.setFormat(EN);
    //			cout << "\t" << test2.toString()
    //				<< "\tKW: " << test2.getWeekInYear()
    //				<< "\tTag: " << test2.getDayInWeek()
    //				<< endl;
    //		} catch (Exception e) { cout << e.toString() << endl; }
    //	}
    //	return 0;
    //}
    

    Date.h

    #ifndef DATE_H
    #define DATE_H
    
    #include <string>
    #include "DateFormat.h"
    using namespace std;
    
    class Date {
    
    private:
    	int _day, _month, _year;
    	DateFormat * _df;
    
    public:
    	Date(int day, int month, int year);
    	Date(DateFormat *dformat, int day, int month, int year);
    	int getDayInYear(); /* Tag des Jahres */ 
    	int getDayInWeek(); /* Wochentag 0-6 = So-Sa*/ 
    	int getWeekInYear(); /* Kalenderwoche */ 
    	bool isLeapYear(); /* schaltjahr? */ 
    	int diff(Date d); /* Abstand der Daten this und d */ 
    	void setFormat(DateFormat *f);/* change format */ 
    	string toString(); /* Date output */ 
    	/* public outputs */ 
    	int getDateDay();
    	int getDateMonth();
    	int getDateYear();
    };
    
    #endif#ifndef DATE_H
    #define DATE_H
    
    #include <string>
    #include "DateFormat.h"
    using namespace std;
    
    class Date {
    
    private:
    	int _day, _month, _year;
    	DateFormat * _df;
    
    public:
    	Date(int day, int month, int year);
    	Date(DateFormat *dformat, int day, int month, int year);
    	int getDayInYear(); /* Tag des Jahres */ 
    	int getDayInWeek(); /* Wochentag 0-6 = So-Sa*/ 
    	int getWeekInYear(); /* Kalenderwoche */ 
    	bool isLeapYear(); /* schaltjahr? */ 
    	int diff(Date d); /* Abstand der Daten this und d */ 
    	void setFormat(DateFormat *f);/* change format */ 
    	string toString(); /* Date output */ 
    	/* public outputs */ 
    	int getDateDay();
    	int getDateMonth();
    	int getDateYear();
    };
    
    #endif
    

    DateFormat.cpp

    #include <iostream>
    #include <sstream>
    #include <iomanip>
    #include <string>
    #include "Date.h"
    #include "DateFormat.h"
    #include "tokenizer.h"
    using namespace std;
    
    /* DateFormatDE: 04.12.2003 *********************************************************************************/ 
    string DateFormatDE::format(Date d) /* ausgeben */ 
    {
    	ostringstream os;
    	os << d.getDateDay() << "."
    		<< d.getDateMonth() << "."
    		<< d.getDateYear();
    	return os.str();
    }
    
    Date DateFormatDE::parse(string s) /* einlesen */ 
    {
    	StringTokenizer date(s, ".");
    	DateFormatDE *DE = new DateFormatDE();
    	int dday = atoi(date.firstToken().c_str());
    	int dmonth = atoi(date.nextToken().c_str());
    	int dyear = atoi(date.nextToken().c_str());
    	Date da(DE, dday, dmonth, dyear);
    	return da;
    }
    
    /* DateFormatEN: 2003-12-04 *********************************************************************************/ 
    string DateFormatEN::format(Date d) /* ausgeben */ 
    {
    	ostringstream os;
    	os << d.getDateYear() << "-"
    		<< d.getDateMonth() << "-"
    		<< d.getDateDay();
    	return os.str();
    }
    
    Date DateFormatEN::parse(string s) /* einlesen */ 
    {
    	StringTokenizer date(s, "-");
    	DateFormatEN *EN = new DateFormatEN();
    	int dyear = atoi(date.firstToken().c_str());
    	int dmonth = atoi(date.nextToken().c_str());
    	int dday = atoi(date.nextToken().c_str());
    	Date da(EN, dday, dmonth, dyear);
    	return da;
    }
    
    /* DateFormatUS: 12/04/2003 *********************************************************************************/ 
    string DateFormatUS::format(Date d) /* ausgeben */ 
    {
    	ostringstream os;
    	os << d.getDateMonth() << "/"
    		<< d.getDateDay() << "/"
    		<< d.getDateYear();
    	return os.str();
    }
    
    Date DateFormatUS::parse(string s) /* einlesen */ 
    {
    	StringTokenizer date(s, "/");
    	DateFormatUS *US = new DateFormatUS();
    	int dmonth = atoi(date.firstToken().c_str());
    	int dday = atoi(date.nextToken().c_str());
    	int dyear = atoi(date.nextToken().c_str());
    	Date da(US, dday, dmonth, dyear);
    	return da;
    }
    

    DateFormat.h

    #ifndef DATEFORMAT_H
    #define DATEFORMAT_H
    
    #include <string>
    #include "Date.h"
    using namespace std;
    
    class Date;
    
    class DateFormat {
    public:
    	virtual string format(Date d) = 0; /* erzeugt standardausgabe */ 
    	virtual Date parse(string s) = 0; /* liest string, liefert Date-Objekt */ 
    };
    
    /* subclass für DE-Darstellung */ 
    class DateFormatDE: public DateFormat {
    public:
    	string format(Date d); /* erzeugt standardausgabe */ 
    	Date parse(string s); /* liest string, liefert Date-Objekt */ 
    };
    
    /* subclass für EN-Darstellung */ 
    class DateFormatEN: public DateFormat {
    public:
    	string format(Date d); /* erzeugt standardausgabe */ 
    	Date parse(string s); /* liest string, liefert Date-Objekt */ 
    };
    
    /* subclass für US-Darstellung */ 
    class DateFormatUS: public DateFormat {
    public:
    	string format(Date d); /* erzeugt standardausgabe */ 
    	Date parse(string s); /* liest string, liefert Date-Objekt */ 
    };
    
    #endif
    

    Exception.cpp

    #include "exception.h"
    using namespace std;
    
    Exception::Exception(string error)
    {
    	_error = error;
    }
    
    string Exception::toString()
    {
    	return _error;
    }
    

    Exception.h

    #ifndef _EXCEPTION_H
    #define _EXCEPTION_H
    
    #include <string>
    using namespace std;
    
    class Exception {
    
    private:
    	string _error;
    
    public:
    	Exception(string error);
    	string toString();
    
    };
    
    #endif
    

    tokenizer.cpp

    //********************************************************************
    //  tokenizer.cpp
    //********************************************************************
    
    #include "tokenizer.h"
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    //********************************************************************
    StringTokenizer::StringTokenizer(string data, string delimiters) {
        _data = data;
        _delimiters = delimiters;
    }
    
    //********************************************************************
    string StringTokenizer::firstToken() {
        char *res;
    
        res = strtok((char *) _data.c_str(), _delimiters.c_str());
        if (res == NULL)
            return "";
        return res;
    }
    
    //********************************************************************
    string StringTokenizer::nextToken() {
        char *res;
    
        res = strtok(NULL, _delimiters.c_str());
        if (res == NULL)
            return "";
        return res;
    }
    

    tokenizer.h

    //********************************************************************
    //  tokenizer.h
    //********************************************************************
    
    #include <string>
    using namespace std;
    
    //********************************************************************
    class StringTokenizer {
    private:
        string _data;
        string _delimiters;
    
    public:
        StringTokenizer(string data, string delim);
        string firstToken();
        string nextToken();
    };
    

  • Administrator

    1. Problembeschreibung: Wo ist das Problem?
    2. Fehlermeldungen? Erwartungen? Resultate?
    3. Du glaubst nicht ernsthaft, dass irgendjemand freiwillig diese Menge an Code durchlesen wird? Reduziere es auf das nötigste!

    http://www.c-plusplus.net/forum/viewtopic-var-t-is-200753.html

    Grüssli



  • naja - wieso auch immer du hier 20 seiten postest... ich bin gerad zu faul, die alle durchzulesen, aber zur main schreib ich dir mal paar worte:

    int main() //in C++ ist es unüblich - und wird oft als schlechter stil angesehen - void in die parameterliste zu schreiben
    { 
        bool ok = true;
    //    string temp; --- nutzt du nie
    
        if (ok &= isLeapYeartest())
           cout << "Schaltjahr Test bestanden \n";
        else
           cout << "Schaltjahr Test fehlgeschlagen\n";
    
        if (ok &= getDayInYeartest())
           cout << "getDayInYear Test bestanden \n";
        else
           cout << "getDayInYear Test fehlgeschlagen\n"; 
    
    //usw
    
    //    return 0; --- unnötig
    }
    

    und noch das:

    /* falsch und schwer lesbar: */
        if(!d.isLeapYear()){
            ok= true;
        }
        else{
            ok=false;
        }
    
        if(e.isLeapYear()){
            ok=true;
        }
        else{
            ok=false;
        }
    
        return ok;
    
    //richtig und man kann es lesen...
    bool ok = !d.isLeapYear() && e.isLeapYear();
    

    bb

    edit:
    DateFormat *de = new DateFormatDE();
    ->
    DateFormat de;
    und dann de-> durch de. ersetzen - du brauchst hier kein new - und hast schon mal wieder wunderbar gezeigt, wie klasse man einfach jedes delete weglassen kann 😉



  • Hi Unskilled

    Habe die Sachen die du erwähnt hast umgeändert. Leider weiss ich aber nicht wie ich es bei der Funktion " Diff" mache. Ich kriege da immer Fehlermeldungen.



  • dann poste die doch mal -.-
    schreib einfach mal nur die fkt difftime hier hin und die fehlermeldungen dazu - dann les ichs mir auch mal durch - und mit sicherheit denken andere da genau so... aber hier wird keiner lust haben, 100de zeilen quelltext durchzulesen...

    bb


  • Administrator

    Dravere schrieb:

    1. Problembeschreibung: Wo ist das Problem?
    2. Fehlermeldungen? Erwartungen? Resultate?
    3. Du glaubst nicht ernsthaft, dass irgendjemand freiwillig diese Menge an Code durchlesen wird? Reduziere es auf das nötigste!

    http://www.c-plusplus.net/forum/viewtopic-var-t-is-200753.html

    Grüssli



  • Dravere schrieb:

    1. Problembeschreibung: Wo ist das Problem?
    2. Fehlermeldungen? Erwartungen? Resultate?
    3. Du glaubst nicht ernsthaft, dass irgendjemand freiwillig diese Menge an Code durchlesen wird? Reduziere es auf das nötigste!

    http://www.c-plusplus.net/forum/viewtopic-var-t-is-200753.html

    Grüssli



  • verdammt - ich hatte schon auch überlegt, es zu machen... aber jz is ja zu spät ^^

    btw: unter der annahme, dass der rest funktioniert:

    //bool difftest(Date d)()
    //{
    //    bool ok;
    //
    //    DateFormat *de = new DateFormatDE();
    //    DateFormat *en = new DateFormatEN();
    //
    //    
    //    Date d(de,20,2,2009);
    //    Date e(de,19,1,2009);
    //
    //    if(d.diff(d)- e.diff(d)== 42{
    //        ok=true;
    //    }
    //    else{
    //        ok=false;
    //    }
    //
    //    return ok;
    //}
    

    -> return (d.diff(d) - e.diff(d)) == 42;
    sieht aber relativ falsch aus : D
    d.diff(e) sieht von der logik her besser aus... aber kA

    bb


Anmelden zum Antworten