testing
This commit is contained in:
parent
57d325d116
commit
7df2412fd3
51
.vscode/settings.json
vendored
Normal file
51
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"iostream": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"list": "cpp",
|
||||
"string": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"random": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"numbers": "cpp",
|
||||
"ostream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"fstream": "cpp",
|
||||
"sstream": "cpp"
|
||||
}
|
||||
}
|
23
2Q_cache.h
23
2Q_cache.h
@ -22,8 +22,6 @@ public:
|
||||
|
||||
void put_page(int id) {
|
||||
if (umap1.find(id) != umap1.end()) { //page is in fast queue in the end, nothing to do (fast case)
|
||||
std::cout<<"fast case\n";
|
||||
|
||||
return;
|
||||
} else if (umap2.find(id) != umap2.end()) {
|
||||
q2.erase(umap2[id]); //if page is in slow put it in fast
|
||||
@ -55,8 +53,9 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void info() {
|
||||
std::cout << "q1 (the most usable): ";
|
||||
void print_info() {
|
||||
|
||||
std::cout << "\nq1 (the most usable): ";
|
||||
for (auto i = q1.begin(); i != q1.end(); i++) {
|
||||
std::cout << *i << " ";
|
||||
}
|
||||
@ -64,6 +63,22 @@ public:
|
||||
for (auto i = q2.begin(); i != q2.end(); i++) {
|
||||
std::cout << *i << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
std::string string_info () { //to easy compare with tests;
|
||||
std::string answer;
|
||||
|
||||
for (auto i = q1.begin(); i != q1.end(); i++) {
|
||||
answer += std::to_string(*i) + " ";
|
||||
}
|
||||
for (auto i = q2.begin(); i != q2.end(); i++) {
|
||||
answer += std::to_string(*i) + " ";
|
||||
}
|
||||
|
||||
answer.pop_back();
|
||||
|
||||
return answer;
|
||||
}
|
||||
};
|
||||
|
||||
|
53
cache_test.cpp
Normal file
53
cache_test.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#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() {
|
||||
cache_test("tests.txt");
|
||||
|
||||
}
|
||||
|
19
main.cpp
19
main.cpp
@ -3,21 +3,8 @@
|
||||
#include "2Q_cache.h"
|
||||
|
||||
|
||||
int main() {
|
||||
TwoCache cache(4, 4);
|
||||
cache.put_page(1);
|
||||
cache.put_page(2);
|
||||
cache.info();
|
||||
cache.put_page(1);
|
||||
cache.put_page(2);
|
||||
cache.info();
|
||||
cache.put_page(1);
|
||||
cache.put_page(2);
|
||||
cache.put_page(1);
|
||||
cache.put_page(2);
|
||||
cache.put_page(3);
|
||||
cache.info();
|
||||
cache.put_page(4);
|
||||
|
||||
int main() {
|
||||
TwoCache cache(2, 4);
|
||||
|
||||
return 0;
|
||||
}
|
9
makefile
9
makefile
@ -5,7 +5,8 @@ CXX = g++
|
||||
|
||||
CXXFLAGS = -Wall -Wextra -std=c++11
|
||||
|
||||
SRCS = main.cpp
|
||||
SRCS = main.cpp
|
||||
TEST = cache_test.cpp
|
||||
|
||||
OBJS = $(SRCS:.cpp=.o)
|
||||
|
||||
@ -17,8 +18,12 @@ $(TARGET): $(OBJS)
|
||||
%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
|
||||
test:
|
||||
$(CXX) $(CXXFLAGS) $(TEST)-o test
|
||||
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
rm -f $(OBJS) $(TARGET) $(TEST)
|
||||
|
||||
run: $(TARGET)
|
||||
./$(TARGET)
|
||||
|
BIN
test_cache
Executable file
BIN
test_cache
Executable file
Binary file not shown.
Loading…
Reference in New Issue
Block a user