cache_realisation_CPP/cache_test.cpp

54 lines
1.6 KiB
C++

#include <iostream>
#include <fstream>
#include "2Q_cache.h"
#include <sstream>
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;
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
for(int i = 0; i < num_of_calls; i++) { //executing
ss >> page_id;
cache.put_page(page_id);
}
std::getline(file, answer_line);
if(cache.string_info() == answer_line) { //compare answers
std::cout << "test - " << test_number << " passed\n";
} else {
std::cout << "test - " << test_number << " failed\n";
std::cout << "right answer - " << answer_line << "your answer - " << cache.string_info() << std::endl;
}
test_number++;
}
file.close();
return 0;
}
int main(int argc, char* argv[]) {
cache_test(argv[1]);
}