EECS 183 Labs

EECS 183 Lab 9: Testing Elevators

Lab due on your scheduled lab day (Nov 7 - Nov 11)

Lab accepted for full credit until Tuesday, November 15, 2022, 11:59 pm Eastern

In this lab, you are creating test cases for the Elevators final project, and learning about testing with classes.

Starter Files

The starter files for the lab are the Elevators project starter files.

You will submit your test.cpp from the Elevators project for grading.

IMPORTANT: For this lab, you will use your Visual Studio or Xcode project that you create for the final project. If you have not yet download the starter code and created a project, do so first - https://eecs183.github.io/fp-elevators/#starter-files

Lab Assignment

Tasks to Complete

  1. If you have not already done so yet, start a new project with your IDE using the starter files for the Elevators final project.
  2. If you have not already done so yet, copy the test cases for the Person class from the specification.
  3. Create more test cases for Person class functions:
    • Person::Person() - non-default constructor
    • Person::tick()
  4. Create test cases for the Elevator class functions:
    • Elevator::tick()
    • Elevator::serviceRequest()
  5. Sumbit your test cases to the autograder
  6. Check your Visual Studio or Xcode project to make sure your text files, like new.in and save.in, are in the correct folder for your project. This task is not graded for the lab but will be essential for you to complete the Core and the Reach for Elevators.

Getting Started with test.cpp

First, you will need to download the Elevators final project starter files and create a project using Visual Studio or Xcode.

NOTE: If you have already started the final project you do not need to do this step again! Use that VS/Xcode project and test.cpp for the lab.

Next, copy the example tests below and paste into the test.cpp file of your project. This is the same test example found in the Testing secion of the Elevators specification.

// declare your test functions here
void person_tests();

void start_tests() {
    // call your test functions here
    person_tests();

    return;
}

// write test functions here
void person_tests() {
    //initialize a person with targetFloor 5, anger 5
    Person p("0f0t5a5");
    cout << p.getTargetFloor() << " " << p.getAngerLevel() << " Expected 5 5" << endl;

    //simulate time being a multiple of TICKS_PER_ANGER_INCREASE
    bool exploded = p.tick(TICKS_PER_ANGER_INCREASE);
    cout << exploded << " Expected 0" << endl;
    cout << p.getTargetFloor() << " " << p.getAngerLevel() << " Expected 5 6" << endl;

    //simulate time not being a multiple
    p.tick(TICKS_PER_ANGER_INCREASE - 1);

    //no change
    cout << p.getTargetFloor() << " " << p.getAngerLevel() << " Expected 5 6" << endl;

    p.tick(TICKS_PER_ANGER_INCREASE); //7 after
    p.tick(TICKS_PER_ANGER_INCREASE); //8 after
    p.tick(TICKS_PER_ANGER_INCREASE); //9 after
    exploded = p.tick(TICKS_PER_ANGER_INCREASE);
    cout << exploded << " Expected 1" << endl;

}

IMPORTANT: Compile and run your project to make sure that there are no errors before moving on!

Once your test cases compile and run, submit your test.cpp to the autograder. The above tests should find two of the autograder bugs:

Note: ctor is an abbreviation for constructor. If your sumbission does not expose these bugs, repeat the setup and try again.

Once you have successfully added the above test cases, start creating more tests for the Person and Elevator classes.

Person Class

You will need to create more test cases for the functions Person::Person() - non-default constructor, and Person::tick()

NOTE: For your test cases, there is also a Person::print() function that you can use to print the values of a Person variable. _It is not implemented - you will have to implement ``Person::print()` to use in in your tests.

/**
 * Requires: nothing
 * Modifies: outs
 * Effects:  Prints the infoString of the person NOT including the turn
 * 
 * Example:  Person p("7f4t8a3");
 *           cout << "\nThe person you just entered is: ";
 *           p.print(cout);
 * 
 *           This will print "f4t8a3"
*/
void print(ostream &outs);

Note: For Person class variables, you can use multiple statements like the following to print a Person variable using cout.

// both of these test cases require implementing Person::print() first!
Person sal("0f0t5a5");
sal.print(cout);
cout << endl;

Person pal("0f0t5a5");
cout << pal << endl;

Elevator Class

You will need to create test cases for the functions Elevator::tick() and Elevator::serviceRequest().

NOTE: You cannot use the overloaded operator to print an Elevator. However, there is an Elevator::print() function which has already been implemented for you.

Here is an example of an Elevator test case for the default constructor, to get you started.

void elevator_tests() {

    // NOTE - there is an Elevator::print function!

    // this will not find any of the bugs for the lab assignment
    Elevator e1;
    e1.print(cout);
    
    return;
}

Do not forget the function declaration before start_tests() void elevator_tests();, and to call elevator_tests(); in start_tests()

 
// declare your test functions here
void person_tests();
void elevator_tests();
C
void start_tests() {
    // call your test functions here
    person_tests();
    elevator_tests();

    return;
}

File IO and Project Setup

It is imperative that your Xcode/Visual studio project is set up correctly to read and write text files. Be sure to edit the scheme for Xcode and that the files are in the correct directory in Visual Studio.

Here is a test case that you can add to your test.cpp to check if your text files, like save.in and new.in, are in the correct directory. Be sure to call file_check in your start_tests function!

void file_check() {

    // open one of the text files that are part of the starter code
    ifstream ins;
    ins.open("new.in");

    // if the file was not in the correct directory, the stream state is fail
    if (ins.fail()) {
        cout << "Could not open new.in" << endl;
    }
    else {
        cout << "Success! Text files are in the right directory." << endl;
    }

    return;
}

If you get the message "Could not open new.in" try editing the scheme again (Mac) or checking that you have the project files in the correct directory (Windows).

If you cannot get the text files in the correct directory, then try adding the following to your file_check test.

// if the file is not in the right directory, try this:
ofstream outs;
outs.open("crazyfilename");
outs << "find the file named crazyfilename in windows explorer or finder";
outs.close();

Then open Windows Explorer (Windows) or Finder (Mac) and search for the file named crazyfilename. Once you find the folder with that file name, copy all of the .in files from the project starter files to that directory. Then try the file_check test again.

How to Submit

IMPORTANT: For all labs in EECS 183, to receive a grade, every student must individually submit the Lab Submission. Late submission for Labs will not be accepted for credit. For this lab, you will receive ten submissions per day with feedback.