diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..878e505 --- /dev/null +++ b/.vscode/settings.json @@ -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" + } +} \ No newline at end of file diff --git a/2Q_cache.h b/2Q_cache.h index b4b4552..72f89e0 100644 --- a/2Q_cache.h +++ b/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; } }; diff --git a/Q2 b/Q2 new file mode 100755 index 0000000..14b3896 Binary files /dev/null and b/Q2 differ diff --git a/cache_test.cpp b/cache_test.cpp new file mode 100644 index 0000000..ef2c685 --- /dev/null +++ b/cache_test.cpp @@ -0,0 +1,53 @@ +#include +#include +#include "2Q_cache.h" +#include + + +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"); + +} + diff --git a/ideal_cache.cpp b/ideal_cache.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/main.cpp b/main.cpp index 676cb03..7ee3775 100644 --- a/main.cpp +++ b/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; } \ No newline at end of file diff --git a/makefile b/makefile index cc00984..059747e 100644 --- a/makefile +++ b/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) diff --git a/test b/test new file mode 100755 index 0000000..eeefe47 Binary files /dev/null and b/test differ diff --git a/test_cache b/test_cache new file mode 100755 index 0000000..bd74d82 Binary files /dev/null and b/test_cache differ diff --git a/tests.txt b/tests.txt index e69de29..05bb524 100644 --- a/tests.txt +++ b/tests.txt @@ -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 \ No newline at end of file