Ejemplo n.º 1
0
Reaching *reaching(Cfg *cfg)
{
	Bitset **in, **out;
	Bitset **gen, **kill;
	Bitset *bbin, *bbout;
	Reaching *reaching;
	size_t **defs;        /* mapping from did => [def,list] */
	size_t *ndefs;      
	size_t i, j;
	int changed;

	in = zalloc(cfg->nbb * sizeof(Bb*));
	out = zalloc(cfg->nbb * sizeof(Bb*));
	gen = zalloc(cfg->nbb * sizeof(Bb*));
	kill = zalloc(cfg->nbb * sizeof(Bb*));
	defs = zalloc(ndecls * sizeof(size_t*));
	ndefs = zalloc(ndecls * sizeof(size_t));

	collectdefs(cfg, defs, ndefs);
	for (i = 0; i < cfg->nbb; i++) {
		in[i] = mkbs();
		out[i] = mkbs();
		gen[i] = mkbs();
		kill[i] = mkbs();
		if (cfg->bb[i])
			genkill(cfg->bb[i], defs, ndefs, gen[i], kill[i]);
	}

	do {
		changed = 0;
		for (i = 0; i < cfg->nbb; i++) {
			if (!cfg->bb[i])
				continue;
			bbin = mkbs();
			for (j = 0; bsiter(cfg->bb[i]->pred, &j); j++)
				bsunion(bbin, out[j]);
			bbout = bsdup(bbin);
			bsdiff(bbout, kill[i]);
			bsunion(bbout, gen[i]);

			if (!bseq(out[i], bbout) || !bseq(in[i], bbin)) {
				changed = 1;
				bsfree(in[i]);
				bsfree(out[i]);
				in[i] = bbin;
				out[i] = bbout;
			}
		}
	} while (changed);


	reaching = xalloc(sizeof(Reaching));
	reaching->in = in;
	reaching->out = out;
	reaching->defs = defs;
	reaching->ndefs = ndefs;
	return reaching;
}
bool test_WholeNumSeq_writenext(
		const std::vector<uintmax_t>& vals,
		std::size_t storage_size = 1
	)
{
	static std::vector<uint8_t> storage;
	storage.assign(storage_size, 0xFF);

	WNSeq_t wnseq(BitSeq_t(storage.data(), storage.data() + storage.size()));
	if (!write_nums_to_wnumseq<WNSeq_t>(wnseq, vals)){
		return false;
	}

	BitSeq_t bseq(storage.data(), storage.data()+storage.size());
	if (!check_bits_against_seq<WNSeq_t>(bseq, vals)) {
		throw std::logic_error(
			"written values do not match original sequence");
	}

	return true;
}
void WholeNumSequence_test_readwritenext_stress() {
    std::cout << "Test Routine:\tWholeNumSequence class" << std::endl;
 	std::cout << "\ttargets: operators <<, >>; has_next/fits_next methods"
            << std::endl;
 	std::cout << "\ttype:\tstress" << std::endl;

    std::cout << "Allocating " << SIZE << " bytes in memory... ";
	uint8_t storage[SIZE];
	std::cout << "done." << std::endl;

    WNSeq_t wnseq;
    std::vector<uintmax_t> wnums_copy1, wnums_copy2;
    uintmax_t n;
    std::size_t i;
    bool test_status = true;

    std::cout << "Beginning test." << std::endl;
    while(test_status) {
		// Reset storage values
        std::fill(storage, storage+SIZE, -1);
		wnums_copy1.clear();
		wnums_copy2.clear();
        // Fill number stream with random numbers
		std::cout << "Generating/storing random inputs..." << std::endl;
        wnseq.init(BitSeq_t(storage, storage+SIZE));
        while (n = 1 + (rand() % (uintmax_t(1)<<std::min(SIZE, std::size_t(3)))),//CHAR_BIT*sizeof(uintmax_t)-1))),
				wnseq.write_next(n)) {
            std::cout << '\t' << n << ',';
            wnums_copy1.push_back(n);
        }
        std::cout << "\ndone." << std::endl;
        // Retrieve all numbers from stream
        std::cout << "Reading output list from stream...";
        wnseq.init(BitSeq_t(storage, storage+SIZE));
        while (wnseq.read_next(n)) {
            wnums_copy2.push_back(n);
        }
        std::cout << "done." << std::endl;
        // Compare list copies
        std::cout << "Comparing input/output lists... ";
        test_status = (wnums_copy1 == wnums_copy2);
        if (test_status) {
            std::cout << "Success! (input = output)" << std::endl;
        } else {
            std::cout << "Failure! Output list:" << std::endl;
            for (i = 0; i < std::min(wnums_copy1.size(), wnums_copy2.size()); ++i) {
                std::cout << '\t' << (wnums_copy1[i] != wnums_copy2[i] ? "*":"")
                        << wnums_copy2[i] << ',';
            }
            for (; i < wnums_copy2.size(); ++i) {
                std::cout << "\t*" << wnums_copy2[i] << ',';
            }
            std::cout << std::endl;
			std::cout << "Bits Stored: ";
			BitSeq_t bseq(storage, storage+SIZE);
			while (bseq.has_next()) {
				std::cout << (bseq.read_next() ? '1':'0');
			}
			std::cout << std::endl;
        }
    }
    std::cout << "Exiting test." << std::endl;
}