SortedListTests.vala 3.21 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* Copyright 2009-2011 Yorba Foundation
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
 * See the COPYING file in this distribution. 
 */

#if ENABLE_TESTS

public class SortedListTests : Valadate.Fixture, Object {
10 11 12 13 14 15 16 17 18 19 20
    
    public int64 int_comparator(void *a, void *b) {
        int a_prime = (int) a;
        int b_prime = (int) b;
        
        if (a_prime > b_prime) return 1;
        if (a_prime < b_prime) return -1;
        return 0;
    }    
    
    public void test_add() {
21 22 23 24 25 26 27 28
        var sl = new SortedList<int>();
        sl.add<int>(1);
        sl.add<int>(2);
        sl.add<int>(3);
        
        assert(sl.get_count() == 3);
    }

29
    public void test_remove() {
30 31 32 33 34 35 36 37 38 39
        var sl = new SortedList<int>();
        sl.add<int>(1);
        sl.add<int>(2);
        sl.add<int>(3);
        
        sl.remove<int>(3);
        
        assert(sl.get_count() == 2);
    }

40
    public void test_remove_nonexistant() {
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
        var sl = new SortedList<int>();
        sl.add<int>(1);
        sl.add<int>(2);
        sl.add<int>(3);
        
        sl.remove<int>(4);
        
        assert(sl.get_count() == 3);
    }    
    
    public void test_copy() {
        Rand r = new Rand();
        var sl = new SortedList<int>();
        var sl_copy = new SortedList<int>();
        
        for(int i = 0; i < 1000; i++) {
            sl.add<int>((int)r.next_int());
        }
    
        sl_copy = sl.copy();

        for(int i = 0; i < 1000; i++) {
            assert(sl.get_at(i) == sl_copy.get_at(i));
        }
    }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    public void test_contains() {
        Rand r = new Rand();
        var sl = new SortedList<int>();
        
        // set up list of 1000 elements, and place 'sentinel' 
        // value near the middle, but away from it.
        for(int i = 0; i < 496; i++) {
            sl.add<int>((int)r.next_int());
        }
        sl.add(-1);
        
        for(int i = 0; i < 505; i++) {
            sl.add<int>((int)r.next_int());
        }
    
        assert(sl.contains<int>(-1));
    }
    
    public void test_is_empty() {
        var sl = new SortedList<int>();
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
        for(int i = 0; i < 10; i++) {
            sl.add<int>(3);
        }
        
        assert(!sl.is_empty);

        for(int i = 0; i < 10; i++) {
            sl.remove<int>(3);
        }

        assert(sl.is_empty);
    }    
    
    public void test_remove_all() {
        var sl = new SortedList<int>(int_comparator);
        var col = new Gee.ArrayList<int>();
        
        col.add(3);
        
        for(int i = 0; i < 1000; i++) {
            sl.add<int>(3);
        }        
        
        assert(!sl.is_empty);
        
        for(int i = 0; i < 1000; i++) {
            sl.remove_all(col);
        }
        
        assert(sl.is_empty);
    }
    
    public void test_resort() {
        Rand r = new Rand();
        var sl = new SortedList<int>(int_comparator);
        
        for(int i = 0; i < 1000; i++) {
            sl.add<int>((int)r.next_int());
        }

        sl.resort(int_comparator);
        
        for(int i = 1; i < sl.get_count(); i++) {
            int a = sl.get_at(i - 1);
            int b = sl.get_at(i);
            assert(int_comparator((void *)a, (void *)b) < 0);
        }
    }
136 137 138
}

#endif