Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // (c) 2013-2019 David Gerstl, all rights reserved
- // For the use of my C++ students to use as a base to implement
- // dynamic arrays, exceptions and operator overloading, and templates
- // Class farmingdale::stack: General integer stack implementation based on array.
- // We will add dynamic arrays (for unlimited size), templates (to allow multiple types) etc.
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <stdlib.h>
- #include <climits>
- #include <random>
- #include "mysteryStack.h"
- #include "BCS370_arrayBasedStack.h"
- //MysteryStackTest
- bool normalTest();
- bool copyCtorTest();
- //ArrayBasedStackTest
- bool normalTest(int testSize);
- bool copyCtorTest(int test);
- //TestStream
- int testStream(std::string fileName);
- //RandomStream
- void randomStream(std::string fileName, int length);
- int main() {
- //MysteryStackTest
- bool failed = false;
- failed = normalTest();
- if (!failed) {
- copyCtorTest();
- }
- if (false == failed) {
- std::cout << "MysterStack: Passed the [non-exhaustive] test." << std::endl;
- }
- //ArrayBasedStackTest
- failed = false;
- failed = normalTest(100);
- if (!failed) {
- copyCtorTest(100);
- }
- if (false == failed) {
- std::cout << "ArrayBasedStack: Passed the [non-exhaustive] test." << std::endl;
- }
- char selection;
- std::string fileName;
- int length = 0;
- int failLine = 0;
- do
- {
- std::cout << " Main Menu" << std::endl;
- std::cout << " ====================================" << std::endl;
- std::cout << " 1. Exit" << std::endl;
- std::cout << " 2. RandomStream" << std::endl;
- std::cout << " 3. TestStream" << std::endl;
- std::cout << " 4. RandomStream & TestStream" << std::endl;
- std::cout << "\n" << std::endl;
- std::cout << " X. Exit\n" << std::endl;
- std::cout << " ====================================" << std::endl;
- std::cout << " Enter your selection: " << std::endl;
- std::cin >> selection;
- std::cout << std::endl;
- system("cls");
- switch (selection)
- {
- case '1':
- std::cout << "Goodbye.\n";
- system("pause");
- return 0;
- break;
- case '2':
- std::cout << "Please input a filename:" << std::endl;
- std::cin >> fileName;
- std::cout << "How many iterations:" << std::endl;
- std::cin >> length;
- randomStream(fileName, length);
- system("pause");
- system("cls");
- break;
- case '3':
- std::cout << "Please input a filename you would like to run testStream on:" << std::endl;
- std::cin >> fileName;
- int failLine;
- failLine = testStream(fileName);
- if (failLine != 0)
- {
- std::cout << "Error on line " << failLine << std::endl;
- }
- system("pause");
- system("cls");
- break;
- case '4':
- std::cout << "Please input a filename:" << std::endl;
- std::cin >> fileName;
- std::cout << "How many iterations:" << std::endl;
- std::cin >> length;
- randomStream(fileName, length);
- failLine = testStream(fileName);
- if (failLine != 0)
- {
- std::cout << "Error on line " << failLine << std::endl;
- }
- system("pause");
- system("cls");
- break;
- default:
- system("cls");
- std::cout << selection << " is not a valid menu item.\n";
- }
- } while (selection != 1);
- return 0;
- }
- void randomStream(std::string fileName, int length)
- {
- std::ofstream outFile;
- outFile.open(fileName);
- if (outFile)
- {
- std::random_device rd;
- std::mt19937 gen(rd());
- std::uniform_int_distribution<> dis(0, 5);
- for (int i = 0; i < length; ++i)
- {
- int randNum = dis(gen);
- std::uniform_int_distribution<> dis(INT_MIN, INT_MAX);
- int minMax = 0;
- if (randNum == 1) {
- std::uniform_int_distribution<> dis(INT_MIN, INT_MAX);
- int minMax = dis(gen);
- outFile << "A " << minMax << std::endl;
- }
- else if (randNum == 2) {
- outFile << "D" << std::endl;
- }
- else if (randNum == 3) {
- outFile << "P" << std::endl;
- }
- else if (randNum == 4) {
- outFile << "E" << std::endl;
- }
- else if (randNum == 5) {
- outFile << "C" << std::endl;
- }
- else {
- outFile << "S" << std::endl;
- }
- }
- }
- }
- int testStream(std::string fileName)
- {
- //Open file reader
- std::ifstream inFile;
- inFile.open(fileName);
- if (!inFile)
- {
- std::cout << "Error opening file!" << std::endl;
- system("pause");
- }
- //Create the stacks
- farmingdale::stack s1;
- farmingdale::stack s2;
- mystery::stack m1(0);
- char x;
- std::string num;
- int lineNum = 1;
- std::string pe1, pe2, pe3, po1, po2, po3;
- bool failed;
- int failCount = 0;
- while (inFile >> x)
- {
- switch (x)
- {
- case 'A':
- failCount = 0;
- inFile >> num;
- failed = s1.push(num);
- if (false == failed) {
- failCount++;
- }
- failed = s2.push(num);
- if (false == failed) {
- failCount++;
- }
- failed = m1.push(num);
- if (false == failed) {
- failCount++;
- }
- if (failCount == 1 || failCount == 2)
- {
- return lineNum;
- }
- // std::cout << num << std::endl;
- break;
- case 'P':
- failCount = 0;
- failed = s1.peek(pe1);
- if (false == failed) {
- failCount++;
- }
- failed = s2.peek(pe2);
- if (false == failed) {
- failCount++;
- }
- failed = m1.peek(pe3);
- if (false == failed) {
- failCount++;
- }
- if (failCount == 1 || failCount == 2 || (pe1 != pe2) || (pe1 != pe3) || (pe2 != pe3))
- {
- std::cout << failCount << std::endl;
- return lineNum;
- }
- break;
- case 'D':
- failCount = 0;
- failed = s1.pop(po1);
- if (false == failed) {
- failCount++;
- }
- failed = s2.pop(po2);
- if (false == failed) {
- failCount++;
- }
- failed = m1.pop(po3);
- if (false == failed) {
- failCount++;
- }
- if (failCount == 1 || failCount == 2 || (po1 != po2) || (po1 != po3) || (po2 != po3))
- {
- return lineNum;
- }
- break;
- case 'C':
- // std::cout << x << std::endl;
- break;
- case 'S':
- // std::cout << x << std::endl;
- break;
- case 'E':
- // std::cout << x << std::endl;
- break;
- }
- lineNum++;
- }
- std::string pee;
- s1.peek(pee);
- std::cout << pee << std::endl;
- s2.peek(pee);
- std::cout << pee << std::endl;
- m1.peek(pee);
- std::cout << pee << std::endl;
- return 0;
- }
- //MysteryStackTest
- bool normalTest() {
- mystery::stack s1(16);
- for (int i = 0; i < s1.stackCapacity(); ++i) {
- if (mystery::FAILURE == s1.push(std::to_string(i + 1000))) {
- std::cerr << "Error on line " << __LINE__ << std::endl;
- return true;
- }
- }
- if (mystery::SUCCESS == s1.push(std::to_string(s1.stackCapacity() + 1000))) {
- std::cerr << "Error on line " << __LINE__ << std::endl;
- return true;
- }
- std::string j;
- std::string t;
- for (int i = 0; i < s1.stackCapacity(); ++i) {
- if (mystery::FAILURE == s1.peek(t) || mystery::FAILURE == s1.pop(j) ||
- t != j || j != std::to_string(1000 + s1.stackCapacity() - i - 1)) {
- std::cerr << "Error on line " << __LINE__ << " j is " << j << " and i is " << i
- << " and t is " << t << std::endl;
- return true;
- }
- }
- return false;
- }
- //MysteryStackTest
- bool copyCtorTest() {
- mystery::stack s1(16);
- for (int i = 0; i < s1.stackCapacity(); ++i) {
- if (mystery::FAILURE == s1.push(std::to_string(i + 1000))) {
- std::cerr << "Error on line " << __LINE__ << std::endl;
- return true;
- }
- }
- if (mystery::SUCCESS == s1.push(std::to_string(s1.stackCapacity() + 1000))) {
- std::cerr << "Error on line " << __LINE__ << std::endl;
- return true;
- }
- // copy s1 into s2
- mystery::stack s2(s1);
- std::string j;
- std::string t;
- for (int i = 0; i < s2.stackCapacity(); ++i) {
- if (mystery::FAILURE == s2.peek(t) || mystery::FAILURE == s2.pop(j) ||
- t != j || j != std::to_string(1000 + s2.stackCapacity() - i - 1)) {
- std::cerr << "Error on line " << __LINE__ << " j is " << j << " and i is " << i
- << " and t is " << t << std::endl;
- return true;
- }
- }
- return false;
- }
- //ArrayBasedStackTest
- bool normalTest(int testSize) {
- farmingdale::stack s1;
- for (int i = 0; i < testSize; ++i) {
- if (farmingdale::FAILURE == s1.push(std::to_string(i + 1000))) {
- std::cerr << "Error on line " << __LINE__ << std::endl;
- return true;
- }
- }
- // if (farmingdale::SUCCESS == s1.push(std::to_string(s1.stackCapacity() + 1000))) {
- // std::cerr << "Error on line " << __LINE__ << std::endl;
- // return true;
- // }
- std::string j;
- std::string t;
- for (int i = 0; i < testSize; ++i) {
- if (farmingdale::FAILURE == s1.peek(t) || farmingdale::FAILURE == s1.pop(j) ||
- t != j || j != std::to_string(1000 + testSize - i - 1)) {
- std::cerr << "Error on line " << __LINE__ << " j is " << j << " and i is " << i
- << " and t is " << t << std::endl;
- return true;
- }
- }
- return false;
- }
- //ArrayBasedStackTest
- bool copyCtorTest(int testSize) {
- farmingdale::stack s1;
- for (int i = 0; i < testSize; ++i) {
- if (farmingdale::FAILURE == s1.push(std::to_string(i + 1000))) {
- std::cerr << "Error on line " << __LINE__ << std::endl;
- return true;
- }
- }
- // if (farmingdale::SUCCESS == s1.push(std::to_string(s1.stackCapacity() + 1000))) {
- // std::cerr << "Error on line " << __LINE__ << std::endl;
- // return true;
- // }
- // copy s1 into s2
- farmingdale::stack s2(s1);
- if (s1 != s2) {
- std::cerr << "Error on line " << __LINE__ << std::endl;
- return true;
- }
- std::string j;
- std::string t;
- for (int i = 0; i < testSize; ++i) {
- if (farmingdale::FAILURE == s2.peek(t) || farmingdale::FAILURE == s2.pop(j) ||
- t != j || j != std::to_string(1000 + testSize - i - 1)) {
- std::cerr << "Error on line " << __LINE__ << " j is " << j << " and i is " << i
- << " and t is " << t << std::endl;
- return true;
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement