ds.cpp 3.13 KB
Newer Older
1
/*
2
 * Copyright 2011, Ben Langmead <langmea@cs.jhu.edu>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This file is part of Bowtie 2.
 *
 * Bowtie 2 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bowtie 2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Bowtie 2.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ds.h"

MemoryTally gMemTally;
23

24 25 26 27
/**
 * Tally a memory allocation of size amt bytes.
 */
void MemoryTally::add(int cat, uint64_t amt) {
28
	ThreadSafe ts(mutex_m);
29 30 31 32 33 34 35 36 37 38 39 40 41 42
	tots_[cat] += amt;
	tot_ += amt;
	if(tots_[cat] > peaks_[cat]) {
		peaks_[cat] = tots_[cat];
	}
	if(tot_ > peak_) {
		peak_ = tot_;
	}
}

/**
 * Tally a memory free of size amt bytes.
 */
void MemoryTally::del(int cat, uint64_t amt) {
43
	ThreadSafe ts(mutex_m);
44 45 46 47 48 49
	assert_geq(tots_[cat], amt);
	assert_geq(tot_, amt);
	tots_[cat] -= amt;
	tot_ -= amt;
}
	
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
#ifdef MAIN_DS

#include <limits>
#include "random_source.h"

using namespace std;

int main(void) {
	cerr << "Test EHeap 1...";
	{
		EHeap<float> h;
		h.insert(0.5f);  // 1
		h.insert(0.6f);  // 2
		h.insert(0.25f); // 3
		h.insert(0.75f); // 4
		h.insert(0.1f);  // 5
		h.insert(0.9f);  // 6
		h.insert(0.4f);  // 7
		assert_eq(7, h.size());
		if(h.pop() != 0.1f) {
			throw 1;
		}
		assert_eq(6, h.size());
		if(h.pop() != 0.25f) {
			throw 1;
		}
		assert_eq(5, h.size());
		if(h.pop() != 0.4f) {
			throw 1;
		}
		assert_eq(4, h.size());
		if(h.pop() != 0.5f) {
			throw 1;
		}
		assert_eq(3, h.size());
		if(h.pop() != 0.6f) {
			throw 1;
		}
		assert_eq(2, h.size());
		if(h.pop() != 0.75f) {
			throw 1;
		}
		assert_eq(1, h.size());
		if(h.pop() != 0.9f) {
			throw 1;
		}
		assert_eq(0, h.size());
		assert(h.empty());
	}
	cerr << "PASSED" << endl;

	cerr << "Test EHeap 2...";
	{
		EHeap<size_t> h;
		RandomSource rnd(12);
105
		size_t lim = 2000;
106 107 108 109
		while(h.size() < lim) {
			h.insert(rnd.nextU32());
		}
		size_t last = std::numeric_limits<size_t>::max();
110
		bool first = true;
111 112
		while(!h.empty()) {
			size_t p = h.pop();
113
			assert(first || p >= last);
114
			last = p;
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
			first = false;
		}
	}
	cerr << "PASSED" << endl;

	cerr << "Test EBitList 1...";
	{
		EBitList<128> l;
		assert_eq(0, l.size());
		assert_eq(std::numeric_limits<size_t>::max(), l.max());
		
		assert(!l.test(0));
		assert(!l.test(1));
		assert(!l.test(10));
		
		for(int i = 0; i < 3; i++) {
			l.set(10);
			assert(!l.test(0));
			assert(!l.test(1));
			assert(!l.test(9));
			assert(l.test(10));
			assert(!l.test(11));
		}
		
		assert_eq(10, l.max());
		l.clear();
		assert(!l.test(10));
		assert_eq(std::numeric_limits<size_t>::max(), l.max());
		
		RandomSource rnd(12);
		size_t lim = 2000;
		for(size_t i = 0; i < lim; i++) {
			uint32_t ri = rnd.nextU32() % 10000;
			l.set(ri);
			assert(l.test(ri));
150 151 152 153 154 155
		}
	}
	cerr << "PASSED" << endl;
}

#endif /*def MAIN_SSTRING*/