bsl_test_queue.h 6.3 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <deque>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>

#include <bsl/deque.h>
#include <bsl/string.h>
#include <bsl/list.h>
#include <bsl/pool.h>
#include <cxxtest/TestSuite.h>

#include <bsl/archive/bsl_serialization.h>
#include <bsl/archive/bsl_filestream.h>
#include <bsl/archive/bsl_binarchive.h>

std::string rand_string()
{
    std::string str;
    srand(time(NULL));
    int n = rand()%100;
    for (int i = 0; i < n; ++i) {
        str += 'a' + rand()%26;
    }
    return str;
}

struct MyClass {
    public:
    char name[800];
    char value[800];
    bool operator == (const MyClass & __x) const {
        return ((strcmp(name, __x.name) == 0)&&
                (strcmp(value, __x.value) == 0));
    }
    bool operator < (const MyClass & __x) const{
        return ((strcmp(name, __x.name) < 0) || 
                ((strcmp(name, __x.name) == 0) &&
                (strcmp(value, __x.value) < 0)));
    }
    const MyClass & operator = (const MyClass &__x) {
        memcpy(name, __x.name, sizeof(name));
        memcpy(value, __x.value, sizeof(value));
        memcpy(u, __x.u, 256);
        return *this;
    }
    char *u;
    MyClass() {
        u = (char*)malloc(256);
        memset(u, 0, 128);
    }
    ~MyClass() {
        free(u);
    }
    MyClass(const MyClass &__x) {
        memcpy(name, __x.name, sizeof(name));
        memcpy(value, __x.value, sizeof(value));
        u = (char*)malloc(256);
        memcpy(u, __x.u, 256);
    }
};


template<typename Tp>
Tp rand_value();

template<>
int rand_value<int>(){
    return rand();
}

template<>
std::string rand_value<std::string>(){
    return rand_string();
}

template<>
MyClass rand_value<MyClass>(){
    MyClass ret;
    std::string name = rand_string();
    std::string value = rand_string();
    memcpy(ret.name, name.c_str(), name.size());
    memcpy(ret.value, value.c_str(), value.size());
    return ret;
}


template<typename Tp, typename Container>
void test_queue(){
    typedef Tp val_type;
    typedef std::queue<val_type, Container> Queue;

    const int N = 10;
    const int M = 5;
    std::vector<val_type> vec(N);
    for(int i = 0; i < N; ++i){
        vec[i] = rand_value<Tp>();
    }

    //
    Queue q;
    TS_ASSERT(q.size() == 0);
     
    //push
    for(int i = 0; i < N; ++i){
        q.push(vec[i]);
        TS_ASSERT(q.back() == vec[i]);
        TS_ASSERT(q.front() == vec[0]);
    }
    TS_ASSERT((int)q.size() == N);

    //pop (N-M) items
    for(int i = 0; i < N - M; ++i){
        TS_ASSERT(q.front() == vec[i]);
        TS_ASSERT(q.back() == vec[N - 1]);
        q.pop();
    }
    TS_ASSERT((int)q.size() == M);

    //const queue reference
    const Queue& cq = q;
    TS_ASSERT(cq.empty() == false);
    TS_ASSERT((int)cq.size() == M);

    //front item refenrnce
    const val_type& v1 = cq.front();
    val_type& v2 = q.front();
    TS_ASSERT(v1 == v2);
    
    //back item refenrnce
    const val_type& v3 = cq.back();
    val_type& v4 = q.back();
    TS_ASSERT(v3 == v4);

    //copy conqruct
    Queue q2(q);
    for(int i = 0; i < M; ++i){
        TS_ASSERT(q2.front() == vec[N - M + i]);
        TS_ASSERT(q2.back() == vec[N - 1]);
        q2.pop();
    }   
    TS_ASSERT((int)q2.size() == 0);
    TS_ASSERT(q2.empty() == true);

    //q has not been changed.
    TS_ASSERT((int)q.size() == M);
    TS_ASSERT(q.empty() == false);

    //assignment
    Queue q3;
    q3 = q;
    for(int i = 0; i < M; ++i){
        TS_ASSERT(q3.front() == vec[N - M + i]);
        TS_ASSERT(q3.back() == vec[N - 1]);
        q3.pop();
    } 
    TS_ASSERT((int)q3.size() == 0);
    TS_ASSERT(q3.empty() == true);
    
    //q has not been changed.
    TS_ASSERT((int)q.size() == M);
    TS_ASSERT(q.empty() == false);
    return;
}

template<typename Tp, typename Container>
void test_priority_queue(){
    typedef Tp val_type;
    typedef std::priority_queue<val_type, Container> Queue;

    const int N = 10;
    const int M = 5;
    std::vector<val_type> vec(N);
    for(int i = 0; i < N; ++i){
        vec[i] = rand_value<Tp>();
    }

    //
    Queue q;
    TS_ASSERT(q.size() == 0);
     
    //push
    for(int i = 0; i < N; ++i){
        q.push(vec[i]);
        std::sort(vec.begin(), vec.begin() + i + 1);
        TS_ASSERT(q.top() == vec[i]);
    }
    TS_ASSERT((int)q.size() == N);

    std::sort(vec.begin(), vec.end());
    //pop (N-M) items
    for(int i = 0; i < N - M; ++i){ 
        TS_ASSERT(q.top() == vec[N - 1 - i]);
        q.pop();
    }
    TS_ASSERT((int)q.size() == M);

    //const queue reference
    const Queue& cq = q;
    TS_ASSERT(cq.empty() == false);
    TS_ASSERT((int)cq.size() == M);

    //top item refenrnce
    const val_type& v1 = cq.top();
    const val_type& v2 = q.top();
    TS_ASSERT(v1 == v2);

    //copy conqruct
    Queue q2(q);
    for(int i = 0; i < M; ++i){
        q2.pop();
    }   
    TS_ASSERT((int)q2.size() == 0);
    TS_ASSERT(q2.empty() == true);

    //q has not been changed.
    TS_ASSERT((int)q.size() == M);
    TS_ASSERT(q.empty() == false);

    //assignment
    Queue q3;
    q3 = q;
    for(int i = 0; i < M; ++i){
        q3.pop();
    } 
    TS_ASSERT((int)q3.size() == 0);
    TS_ASSERT(q3.empty() == true);
    
    //q has not been changed.
    TS_ASSERT((int)q.size() == M);
    TS_ASSERT(q.empty() == false);
    return;
}



class bsl_test_queue : public CxxTest::TestSuite
{
public:
    //queue
    void test_queue_with_int_deque(){
        test_queue<int, bsl::deque<int> >();
    }
    void test_queue_with_string_deque(){
        test_queue<std::string, bsl::deque<std::string> >();
    }
    void test_queue_with_myclass_deque(){
        test_queue<MyClass, bsl::deque<MyClass> >();
    }
    
    //priority_queue
    void test_priority_queue_with_int_deque(){
        test_priority_queue<int, bsl::deque<int> >();
    }
    void test_priority_queue_with_string_deque(){
        test_priority_queue<std::string, bsl::deque<std::string> >();
    }
    void test_priority_queue_with_myclass_deque(){
        test_priority_queue<MyClass, bsl::deque<MyClass> >();
    }

    void test_queue_with_int_list(){
        test_queue<int, bsl::list<int> >();
    }
    void test_queue_with_string_list(){
        test_queue<std::string, bsl::list<std::string> >();
    }
    void test_queue_with_myclass_list(){
        test_queue<MyClass, bsl::list<MyClass> >();
    }
};