This commit is contained in:
ag 2024-09-15 18:40:48 +03:00
parent 57d325d116
commit 7df2412fd3
10 changed files with 141 additions and 22 deletions

51
.vscode/settings.json vendored Normal file
View 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"
}
}

View File

@ -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;
}
};

BIN
Q2 Executable file

Binary file not shown.

53
cache_test.cpp Normal file
View 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");
}

View File

View File

@ -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;
}

View File

@ -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 Executable file

Binary file not shown.

BIN
test_cache Executable file

Binary file not shown.

View File

@ -0,0 +1,8 @@
1 1 10 1 1 1 1 1 2 2 2 2 3
2 3
3 4 16 1 2 3 4 5 6 1 2 3 1 4 5 3 4 6 7
4 3 1 7 6 5 2
3 10 10 1 2 3 1 2 3 6 8 9 11
3 2 1 11 9 8 6
10 10 20 1 2 3 4 5 6 7 8 9 10 11 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 11