TEST(EmptyDequeTest, Random) { std::deque<int> control; Deque<int> deque; for (size_t i = 0; i < CHUNK_COUNT; ++i) { clock_t time = clock(); for (size_t c = 0; c < CHUNK_SIZE; ++c) { int operand = rand(); switch (rand() % 4) { case 0: deque.push_back(operand); control.push_back(operand); break; case 1: deque.push_front(operand); control.push_front(operand); break; case 2: if (control.size() == 0) { break; } EXPECT_EQ(deque.pop_front(), control.front()); control.pop_front(); break; case 3: if (control.size() == 0) { break; } EXPECT_EQ(deque.pop_back(), control.back()); control.pop_back(); break; } } std::cout << clock() - time << std::endl; } }
TEST(Deque, pop_back) { Deque d; d.push_back(0); d.push_back(1); CHECK(1 == d.pop_back()); CHECK(0 == d.pop_back()); }
TEST(Deque, grow) { Deque d; for (int i = 0; i < 10; i++) d.push_back(i); d.push_back(10); CHECK(11 == d.size()); CHECK(10 == d.pop_back()); }
int main() { cout << "It runs!" << endl; //create a stack & show it works Stack<int> s; s.pop(); s.print(); cout<<"Empty? "<<s.empty()<<endl; cout<<s.size()<<endl; for(int i=0; i<10; i++) { s.push(2*i+1); s.print(); cout<<s.size()<<endl<<endl; } cout<<"Empty? "<<s.empty()<<endl; cout<<"The top is "<<(s.top())<<endl; for(int i=0; i<12; i++) { s.pop(); s.print(); } cout<<"The top is "<<(s.top())<<endl; //create a queue & show it works Queue<int> q; q.print(); cout<<"Empty? "<<q.empty()<<endl; cout<<q.size()<<endl; for(int i=0; i<10; i++) { q.push(2*i+1); q.print(); cout<<q.size()<<endl<<endl; } cout<<"Empty? "<<q.empty()<<endl; cout<<"The front is "<<(q.front())<<endl; cout<<"The rear is "<<(q.back())<<endl; for(int i=0; i<12; i++) { q.pop(); q.print(); } cout<<"The front is "<<(q.front())<<endl; cout<<"The rear is "<<(q.back())<<endl; Deque<int> d; d.print(); for(int i=0; i<10; i++) { d.push_back(i); d.print(); } for(int i=0; i<10; i++) { if(i%2==0) {cout<<"back"<<endl; d.pop_back();} else{cout<<"front"<<endl; d.pop_front();} d.print(); } }
TEST(Deque, doit) { Deque d; d.push_front(0); d.push_back(1); d.push_back(2); d.push_back(3); CHECK(4 == d.size()); CHECK(0 == d.pop_front()); CHECK(3 == d.pop_back()); }
TEST (Deque, double_test) { Deque<double> d; d.push_front(0.1); d.push_back(1.1); d.push_back(2.1); d.push_back(3.1); CHECK ( 4 == d.size() ); CHECK ( 0.1 == d.pop_front() ); CHECK ( 3.1 == d.pop_back() ); }
int main(){ Deque *d = new Deque(); d->push_front(1); d->push_back(2); d->push_back(3); d->push_front(0); d->pop_front(); d->pop_back(); d->print(); delete d; return 0; }
//--------------------Linear solution---------------------- void minimumValues(int v[], int k) { Deque<int> d = Deque<int>(); // In the queue I store positions, no elements for (int i = 0; i < N; i++) { if (i >= k) // Until we have not consider a subarray, we can not print the minimums. cout << v[d.front()] << " "; while (!d.empty() && v[d.back()] >= v[i]) { // While the queue is not empty and the element I want to insert is // greater or equal than the last one I extract the last element d.pop_back(); } d.push_back(i); // I insert the new element if (d.front() <= i - k) //This remove the minimum once is out of the subarray d.pop_front(); } cout << v[d.front()] << endl; }
void test_deque() { const size_t SIZE = 10; Deque<int, SIZE> deque; cout << "--> testing push_back..." << endl; for (size_t i = 0; i < SIZE; i++) { try { deque.push_back(i); } catch (std::overflow_error &e) { cout << "i: " << i << endl; cout << e.what() << endl; } } deque.traverse(print); cout << endl; cout << "size: " << deque.size() << endl; deque.print_mark(); cout << "--> testing pop_back..." << endl; while (!deque.empty()) { deque.pop_back(); } deque.traverse(print); cout << endl; cout << "size: " << deque.size() << endl; deque.print_mark(); cout << "--> testing push_front..." << endl; for (size_t i = 0; i < SIZE; i++) { try { deque.push_front(i); } catch (std::overflow_error &e) { cout << "i: " << i << endl; cout << e.what() << endl; } } deque.traverse(print); cout << endl; cout << "size: " << deque.size() << endl; deque.print_mark(); cout << "--> testing pop_front..." << endl; while (!deque.empty()) { deque.pop_front(); } deque.traverse(print); cout << endl; cout << "size: " << deque.size() << endl; deque.print_mark(); }
int main(int argc, char *argv[]) { Deque *deque; char line[4096]; size_t len; void *data; size_t size; if (argc == 1) { deque = Memory_Deque_create(); } else if (argc == 2 && argv[1][0] != '-') { deque = File_Deque_create(argv[1]); } else { printf("Usage:\n" " test-deque -- use the in-memory deque\n" " test-deque <path> -- use the file-based deque\n" "\n" "Commands:\n" " destroy -- destroy the deque and exit\n" " empty -- report if the deque is empty\n" " size -- print number of elements in the deque\n" " push_back -- add element at the back\n" " push_front -- add element at the front\n" " get_back -- print element at the back\n" " get_front -- print element at the front\n" " pop_back -- remove element at the back\n" " pop_front -- remove element at the front\n"); return 1; } if (deque == NULL) { fprintf(stderr, "Unable to create deque!\n"); exit(1); } while (fgets(line, sizeof(line), stdin) != NULL) { /* Determine length and strip trailing newline character */ len = strlen(line); if (len == 0) break; if (line[len - 1] == '\n') { line[len - 1] = '\0'; len -= 1; } if (strcmp(line, "destroy") == 0) { break; } else if (strcmp(line, "empty") == 0) { printf("empty=%s\n", deque->empty(deque) ? "true" : "false"); } else if (strcmp(line, "size") == 0) { printf("size=%ld\n", (long)deque->size(deque)); } else if (strncmp(line, "push_back ", 10) == 0) { if (!deque->push_back(deque, line + 10, len - 10)) printf("push_back failed!\n"); } else if (strncmp(line, "push_front ", 11) == 0) { if (!deque->push_front(deque, line + 11, len - 11)) printf("push_front failed!\n"); } else if (strcmp(line, "get_back") == 0) { if (!deque->get_back(deque, &data, &size)) printf("get_back failed!\n"); else { fwrite(data, size, 1, stdout); fputc('\n', stdout); } } else if (strcmp(line, "get_front") == 0) { if (!deque->get_front(deque, &data, &size)) printf("get_front failed!\n"); else { fwrite(data, size, 1, stdout); fputc('\n', stdout); } } else if (strcmp(line, "pop_back") == 0) { if (!deque->pop_back(deque)) printf("pop_back failed!\n"); } else if (strcmp(line, "pop_front") == 0) { if (!deque->pop_front(deque)) printf("pop_front failed!\n"); } else { printf("Unrecognized input line: %s!\n", line); } } deque->destroy(deque); return 0; }