Week 2 (CS 250)
Table of Contents
Debugging, Testing, STL
1. Assignment links
| Assignment | Reading link | Canvas link |
|---|---|---|
| 📚 Intro: Testing | Read | Canvas quiz |
| 📚 Intro: General Debugging | Read | Canvas quiz |
| 📚 Intro: Debugger tools (gdb/lldb) | Read | Canvas quiz |
| 📚 Intro: The Standard Template Library | Read | Canvas quiz |
| 💾 Lab: Testing, Debugging, STL | Read | Canvas lab |
| 💬 Discussion: Test Driven Development | Canvas discussion |
2. Example code
2.1. Testing - GetArea program with unit tests
2.1.1. Functions.h
#ifndef _FUNCTIONS // file guards #define _FUNCTIONS // FUNCTION DECLARATIONS float GetArea( float width, float length ); void Test_GetArea(); #endif
2.1.2. Functions.cpp
#include "Functions.h" #include <iostream> using namespace std; // FUNCTION DEFINITIONS float GetArea( float width, float length ) { return width * length; } void Test_GetArea() { { // Test 1 float width = 4; float length = 3; float expectedArea = 12; float actualArea = GetArea( width, length ); if ( actualArea == expectedArea ) { cout << "PASS - Test passed" << endl; } else { cout << "FAIL - Test failed" << endl; cout << "Inputs: width = " << width << endl; cout << " length = " << length << endl; cout << "Expected result: " << expectedArea << endl; cout << "Actual result: " << actualArea << endl; } } { // Test 2 float width = 5; float length = 5; float expectedArea = 25; float actualArea = GetArea( width, length ); if ( actualArea == expectedArea ) { cout << "PASS - Test passed" << endl; } else { cout << "FAIL - Test failed" << endl; cout << "Inputs: width = " << width << endl; cout << " length = " << length << endl; cout << "Expected result: " << expectedArea << endl; cout << "Actual result: " << actualArea << endl; } } }
2.1.3. main.cpp
#include <iostream> #include <iomanip> using namespace std; #include "Functions.h" int main() { // -- TESTS ------------------------- cout << "TESTS" << endl; Test_GetArea(); // -- PROGRAM ----------------------- cout << endl << "PROGRAM" << endl; float width, length, area; cout << "Get width: "; cin >> width; cout << "Get length: "; cin >> length; area = GetArea( width, length ); cout << "Area: " << area << endl; return 0; }