cache_realisation_CPP/cache_test.cpp
2024-09-22 03:25:10 +03:00

71 lines
2.3 KiB
C++

#include <iostream>
#include <fstream>
#include "2Q_cache.h"
#include "Ideal_cache.h"
#include <sstream>
#include <vector>
#include <algorithm>
const char* default_test_file = "tests.txt";
int cache_test(std::string file_name) {
std::ifstream file(file_name);
if(!file.is_open()) {
std::cerr << "File open error" << std::endl; //open test file
return -1;
}
std::string test_line, answer_line;
int fast_q_sz, slow_q_sz, num_of_calls, page_id, test_number = 0, test_passed = 0, test_failed = 0;
while (std::getline(file, test_line)) {
std::stringstream ss(test_line);
ss >> fast_q_sz >> slow_q_sz>>num_of_calls; //read input
TwoCache cache(fast_q_sz, slow_q_sz); //initialising
Ideal_Cache ideal_cache(fast_q_sz + slow_q_sz);
std::vector <int> requests;
for (int i = 0; i < num_of_calls; i++) { //executing
ss >> page_id;
cache.put_page(page_id);
requests.push_back(page_id);
}
ideal_cache.get_request_vector(requests);
std::getline(file, answer_line);
if (cache.string_info() == answer_line) { //compare answers
std::cout << "test - " << test_number << " passed\n";
std::cout << "ideal tics/your tics " << ideal_cache.tic_number << "/" << cache.tic_number << std::endl;
test_passed++;
} else {
std::cout << "test - " << test_number << " failed\n";
std::cout << "right answer - " << answer_line << "your answer - " << cache.string_info() << std::endl;
test_failed++;
}
test_number++;
}
file.close();
std::cout << "\nNumber of tests = "<< test_number << "\nTests passed = " << test_passed << "\nTests failed = " << test_failed << "\n";
return 0;
}
int main(int argc, char* argv[]) {
if (argc == 0) {
cache_test(default_test_file);
} else {
cache_test(argv[1]);
}
}