#include #include #include #include "ubarr.h" /* * Note: This is not something you have to remember, or even understand. * It is what is called a "specialization." A feature of C++ templates * where you can make a generic version of something, then also provide * an implementation for some specific type, in case you want to treat * that type differently. This simply defines a function, used for printing * in the tester, which generally returns its argument. Except if it is a * a string, in which case it returns its argument with double quotes * around it. */ template T pr(const T &d) { return d; } template<> std::string pr(const std::string &d) { return "\"" + d + "\""; } template void test(const std::initializer_list & init1, const std::initializer_list & init2) { unbound_arr ua; // Should be quietly nothing there. std::cout << "Nothing:"; for(int i = -5; i < 30; i += 3) std::cout << " " << pr(ua.get(i)); std::cout << std::endl; // Put some stuff there. auto iscan = init1.begin(); std::cout << "Still Nothing:"; int i = -7; for(auto &x: init1) { // -7 -3 1 5 9 13 17 std::cout << " " << pr(ua.put(i, x)); i += 4; } std::cout << std::endl; // See what happened. std::cout << "Something:"; std::string sep = " "; for(int i = -10; i < 20; ++i) { std::cout << sep << i << ": " << pr(ua.get(i)); sep = ", "; } std::cout << std::endl; // More stuff there. std::cout << "More:"; i = -2; for(auto &x: init2) { // -2 1 4 7 10 13 16 std::cout << " " << pr(ua.put(i, x)); i += 3; } std::cout << std::endl; // See what happened this time. std::cout << "And:"; sep = " "; for(int i = -10; i < 20; ++i) { std::cout << sep << i << ": " << pr(ua.get(i)); sep = ", "; } std::cout << std::endl; } int main() { test({15, 20, -8, 11, 9, 4, -2}, {-18,3,21,9,44,88,13}); std::cout << std::endl; test({"foo","bar","baz","whump","thud","bang","tinkle"}, {"up","down","left","right","over","under","whap"}); }