testing
This commit is contained in:
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) {
|
void put_page(int id) {
|
||||||
if (umap1.find(id) != umap1.end()) { //page is in fast queue in the end, nothing to do (fast case)
|
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;
|
return;
|
||||||
} else if (umap2.find(id) != umap2.end()) {
|
} else if (umap2.find(id) != umap2.end()) {
|
||||||
q2.erase(umap2[id]); //if page is in slow put it in fast
|
q2.erase(umap2[id]); //if page is in slow put it in fast
|
||||||
@ -55,8 +53,9 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void info() {
|
void print_info() {
|
||||||
std::cout << "q1 (the most usable): ";
|
|
||||||
|
std::cout << "\nq1 (the most usable): ";
|
||||||
for (auto i = q1.begin(); i != q1.end(); i++) {
|
for (auto i = q1.begin(); i != q1.end(); i++) {
|
||||||
std::cout << *i << " ";
|
std::cout << *i << " ";
|
||||||
}
|
}
|
||||||
@ -64,6 +63,22 @@ public:
|
|||||||
for (auto i = q2.begin(); i != q2.end(); i++) {
|
for (auto i = q2.begin(); i != q2.end(); i++) {
|
||||||
std::cout << *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");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
15
main.cpp
15
main.cpp
@ -4,20 +4,7 @@
|
|||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
TwoCache cache(4, 4);
|
TwoCache cache(2, 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);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
7
makefile
7
makefile
@ -6,6 +6,7 @@ CXX = g++
|
|||||||
CXXFLAGS = -Wall -Wextra -std=c++11
|
CXXFLAGS = -Wall -Wextra -std=c++11
|
||||||
|
|
||||||
SRCS = main.cpp
|
SRCS = main.cpp
|
||||||
|
TEST = cache_test.cpp
|
||||||
|
|
||||||
OBJS = $(SRCS:.cpp=.o)
|
OBJS = $(SRCS:.cpp=.o)
|
||||||
|
|
||||||
@ -17,8 +18,12 @@ $(TARGET): $(OBJS)
|
|||||||
%.o: %.cpp
|
%.o: %.cpp
|
||||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
test:
|
||||||
|
$(CXX) $(CXXFLAGS) $(TEST)-o test
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJS) $(TARGET)
|
rm -f $(OBJS) $(TARGET) $(TEST)
|
||||||
|
|
||||||
run: $(TARGET)
|
run: $(TARGET)
|
||||||
./$(TARGET)
|
./$(TARGET)
|
||||||
|
BIN
test_cache
Executable file
BIN
test_cache
Executable file
Binary file not shown.
Reference in New Issue
Block a user