Blame testsuite/libdejagnu/unit.cc

Packit 62fe53
// unit.cc -- This is a test case for the dejagnu.h classes.
Packit 62fe53
// Copyright (C) 2001-2016 Free Software Foundation, Inc.
Packit 62fe53
Packit 62fe53
// This file is part of DejaGnu.
Packit 62fe53
Packit 62fe53
// DejaGnu is free software; you can redistribute it and/or modify it
Packit 62fe53
// under the terms of the GNU General Public License as published by
Packit 62fe53
// the Free Software Foundation; either version 3 of the License, or
Packit 62fe53
// (at your option) any later version.
Packit 62fe53
Packit 62fe53
// DejaGnu is distributed in the hope that it will be useful, but
Packit 62fe53
// WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 62fe53
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 62fe53
// General Public License for more details.
Packit 62fe53
Packit 62fe53
// You should have received a copy of the GNU General Public License
Packit 62fe53
// along with DejaGnu; if not, write to the Free Software Foundation,
Packit 62fe53
// Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
Packit 62fe53
Packit 62fe53
#include <sys/types.h>
Packit 62fe53
#include <iostream>
Packit 62fe53
#include <regex.h>
Packit 62fe53
#include <string>
Packit 62fe53
#include <fstream>
Packit 62fe53
#include <sstream>
Packit 62fe53
#include <set>
Packit 62fe53
#include <dejagnu.h>
Packit 62fe53
Packit 62fe53
using namespace std;
Packit 62fe53
Packit 62fe53
TestState runtest;
Packit 62fe53
TestState test;
Packit 62fe53
Packit 62fe53
class TestClass1
Packit 62fe53
{
Packit 62fe53
public:
Packit 62fe53
  string tname;
Packit 62fe53
  unsigned int tnum;
Packit 62fe53
};
Packit 62fe53
Packit 62fe53
TestClass1 testClass1, testClass2, testClass3;
Packit 62fe53
TestClass1 *testClassPtr;
Packit 62fe53
Packit 62fe53
// We have to do this silly crap with renaming the output string, so
Packit 62fe53
// the generic Tcl code that looks for the output state gets confused,
Packit 62fe53
// and records random duplicate messages.
Packit 62fe53
const char *os1[] = {
Packit 62fe53
    "FAI: ", "PAS: ", "UNT: ", "UNR: "
Packit 62fe53
};
Packit 62fe53
Packit 62fe53
const char *os2[] = {
Packit 62fe53
    "FAILED: ", "PASSED: ", "UNTESTED: ", "UNRESOLVED: "
Packit 62fe53
};
Packit 62fe53
Packit 62fe53
int
Packit 62fe53
main (int argc, char *argv[])
Packit 62fe53
{
Packit 62fe53
  regex_t regex_pat;
Packit 62fe53
  outstate = os1;
Packit 62fe53
  stringstream strbuf;
Packit 62fe53
  streambuf *pbuf;
Packit 62fe53
Packit 62fe53
  // Replace the output buffer for cout, so we can examine it to see
Packit 62fe53
  // what was displayed. Otherwise, there is no way we can test the
Packit 62fe53
  // logging functions completely.
Packit 62fe53
  pbuf = cout.rdbuf ();
Packit 62fe53
Packit 62fe53
  testClass1.tname = "testType1";
Packit 62fe53
  testClass1.tnum = 1;
Packit 62fe53
  testClass2.tname = "testType2";
Packit 62fe53
  testClass2.tnum = 2;
Packit 62fe53
  testClass3.tname = "testType3";
Packit 62fe53
  testClass3.tnum = 3;
Packit 62fe53
  
Packit 62fe53
  // Test the pass message.
Packit 62fe53
  cout.rdbuf (strbuf.rdbuf ());
Packit 62fe53
  strbuf.str ("");
Packit 62fe53
  test.pass ("bogus pass message for testing");
Packit 62fe53
  outstate = os2;
Packit 62fe53
  cout.rdbuf (pbuf);
Packit 62fe53
  if (strncmp (strbuf.str().c_str(), "\tPAS: bogus pass message", 22) == 0)
Packit 62fe53
    runtest.pass ("Pass message");
Packit 62fe53
  else
Packit 62fe53
    runtest.fail ("Pass message");
Packit 62fe53
  
Packit 62fe53
  // Test the fail message.
Packit 62fe53
  cout.rdbuf (strbuf.rdbuf ());
Packit 62fe53
  strbuf.str ("");
Packit 62fe53
  outstate = os1;
Packit 62fe53
  test.fail ("bogus fail message for testing");
Packit 62fe53
  cout.rdbuf (pbuf);
Packit 62fe53
  outstate = os2;
Packit 62fe53
  if (strncmp (strbuf.str().c_str(), "\tFAI: bogus fail message", 22) == 0)
Packit 62fe53
    runtest.pass ("Fail message");
Packit 62fe53
  else
Packit 62fe53
    runtest.fail ("Fail message");
Packit 62fe53
  
Packit 62fe53
  // Test the untested message.
Packit 62fe53
  cout.rdbuf (strbuf.rdbuf ());
Packit 62fe53
  strbuf.str ("");
Packit 62fe53
  outstate = os1;
Packit 62fe53
  test.untested ("bogus untested message for testing");
Packit 62fe53
  cout.rdbuf (pbuf);
Packit 62fe53
  outstate = os2;
Packit 62fe53
  if (strncmp (strbuf.str().c_str(), "\tUNT: bogus untested message", 21) == 0) {
Packit 62fe53
    runtest.pass ("Untested message");
Packit 62fe53
  } else {
Packit 62fe53
    runtest.fail ("Untested message");
Packit 62fe53
  }
Packit 62fe53
  
Packit 62fe53
  // Test the unresolved message.
Packit 62fe53
  cout.rdbuf (strbuf.rdbuf ());
Packit 62fe53
  strbuf.str ("");
Packit 62fe53
  outstate = os1;
Packit 62fe53
  test.unresolved ("bogus unresolved message for testing");
Packit 62fe53
  cout.rdbuf (pbuf);
Packit 62fe53
  outstate = os2;
Packit 62fe53
  if (strncmp (strbuf.str().c_str(), "\tUNR: bogus unresolved message", 21) == 0)
Packit 62fe53
    runtest.pass ("Unresolved message");
Packit 62fe53
  else
Packit 62fe53
    runtest.fail ("Unresolved message");
Packit 62fe53
  
Packit 62fe53
  // Make sure we got everything in the totals.
Packit 62fe53
  cout.rdbuf (strbuf.rdbuf ());
Packit 62fe53
  strbuf.str ("");
Packit 62fe53
  regcomp (&regex_pat,
Packit 62fe53
	   "\t#passed.*#real failed.*#untested.*#unresolved",
Packit 62fe53
	   REG_NOSUB);
Packit 62fe53
Packit 62fe53
  test.totals ();
Packit 62fe53
  cout.rdbuf (pbuf);
Packit 62fe53
  if (regexec (&regex_pat, strbuf.str().c_str(), 0, (regmatch_t *) 0, 0) == 0)
Packit 62fe53
    runtest.pass ("Totals message");
Packit 62fe53
  else
Packit 62fe53
    runtest.fail ("Totals message");
Packit 62fe53
Packit 62fe53
  return 0;
Packit 62fe53
}
Packit 62fe53