mardi 25 avril 2023

usleep() behaving in a weird way

I am working on one of my assignments and I have run into an issue.

I wrote my test cases, and I am pushing back the boolean values returned by the functions into a vector.

While trying to print the test results, I ran into an issue with usleep(). I am trying to print out std::cout << "\n\n*** RUNNING ALL TESTS ***"; first, and then pause for a second, and then print the results.

For some reason, my program pauses first, and then prints it all out at once. Is there something I have missed or overlooked?

I am using a makefile to compile all my code, and I made sure the path is correct (I am not working on the wrong file and compiling a completely different file).

p.s Is there a way I can make my runAllTests() look prettier?

A function that runs all tests:

std::vector<bool> ShelterBST::runAllTests(std::vector<bool> &testResults) {
  testResults.push_back(testSearch());
  testResults.push_back(testSearchB());
  testResults.push_back(testInsert()); 
  testResults.push_back(testFindParent());
  testResults.push_back(testFindPredecessor());
  testResults.push_back(testDeleteNode());
  testResults.push_back(testDestroyTree());
  testResults.push_back(testNumberOfChildren());
  testResults.push_back(testNumberOfNodes());
  testResults.push_back(testNumberOfInternalNodes());
  testResults.push_back(testHeight());
  testResults.push_back(testBalance()); 
  testResults.push_back(testNodesAtLevel()); 
  testResults.push_back(testInOrder());
  testResults.push_back(testPreOrder()); 
  testResults.push_back(testPostOrder());
  
  return testResults;
}

The part that includes my question:

void ShelterBST::test() {
  using std::vector;
  #define PASS true
  #define FAIL false
  std::string const TEST = "*** TEST ";
  std::string const PASSED = "  PASSED ***";
  std::string const FAILED = "  - FAILED ###";
  std::string const PASSED_2 = " PASSED ***";
  std::string const FAILED_2 = " - FAILED ###";
  long int const PAUSE = 1000000;

  vector<bool> testValues;
  std::cout << "\n\n*** RUNNING ALL TESTS ***";
  usleep(PAUSE);
  runAllTests(testValues);
  std::cout << std::endl << std::endl;
  for(auto i = 0; i < (int)testValues.size(); i++) {
    if(testValues[i] == PASS && i < 9) {
      std::cout << TEST << i+1 << PASSED << std::endl;

    } else if(testValues[i] == FAIL && i < 9) {
      std::cout << TEST << i+1 << FAILED << std::endl;

    } else if(testValues[i] == PASS && i > 9) {
      std::cout << TEST << i+1 << PASSED_2 << std::endl;

    } else if(testValues[i] == FAIL && i > 9) {
      std::cout << TEST << i+1 << FAILED_2 << std::endl;
    }
  }
}

makefile:

CC=g++
CFLAGS= -g -Wall -Werror -pedantic -std=c++11

SRCS=ShelterBST.cpp assignment3.cpp
OBJS=$(subst .cpp,.o,$(SRCS))

all: prog

prog: $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o prog

%.o: %.cpp
    $(CC) $(CFLAGS) -c $< -o $@

clean:
    rm -f $(OBJS) prog

Aucun commentaire:

Enregistrer un commentaire