int main() { // Seed the random generator srand(time(0)); // Generate random integers with 1 .. 9 digits // We test only 9 digits to avoid overflow std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl; std::cout << "Numbers to test:" << std::endl; for (int i = 0; i < 9; ++i) { numbers[i] = gen_int(i+1); first[i] = numbers[i].c_str(); last[i] = first[i]; while (*last[i]) last[i]++; std::cout << i+1 << " digit number:" << numbers[i] << std::endl; } std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl; BOOST_SPIRIT_TEST_BENCHMARK( 10000000, // This is the maximum repetitions to execute (atoi_test) (strtol_test) (spirit_int_test) ) // This is ultimately responsible for preventing all the test code // from being optimized away. Change this to return 0 and you // unplug the whole test's life support system. return test::live_code != 0; }
void gen_digit(blob data) { int i; gen_int(&i); i %= 10; qc_return(int, i); }
void gen_odd(blob data) { int i; gen_int(&i); if (i % 2 == 0) { i++; } qc_return(int, i); }
void gen_digit_char(blob data) { int i; gen_int(&i); i %= 10; char c = '0' + (char) i; qc_return(char, c); }
int main(int argc, char** argv) { boost::mt19937 generator; generator.seed(time(0) * getpid()); boost::random_number_generator<boost::mt19937> gen(generator); std_set_type stdset; vec_set_type vecset; for (int i = 0; i != 1024 * 16; ++ i) { const int value = gen(1024 * 1024 * 1024); stdset.insert(value); vecset.insert(value); } verify(stdset, vecset); { const size_t size_half = vecset.size() >> 1; while (vecset.size() > size_half) { const int value = *(vecset.begin() + gen(vecset.size())); vecset.erase(value); stdset.erase(value); } } verify(stdset, vecset); for (int i = 0; i != 1024 * 16; ++ i) { const int value = gen(1024 * 1024 * 1024); stdset.insert(value); vecset.insert(value); } verify(stdset, vecset); vecset.clear(); vecset.insert(stdset.begin(), stdset.end()); verify(stdset, vecset); { const size_t size_half = vecset.size() >> 1; while (vecset.size() > size_half) { vec_set_type::iterator viter = vecset.begin() + gen(vecset.size()); const int value = *viter; vecset.erase(viter); stdset.erase(value); } } verify(stdset, vecset); for (int i = 0; i != vecset.size(); ++ i) { const int value = *(vecset.begin() + i); if (vecset.lower_bound(value) != vecset.equal_range(value).first) std::cerr << "different lower bound?" << std::endl; if (vecset.upper_bound(value) != vecset.equal_range(value).second) std::cerr << "different upper bound?" << std::endl; } { const size_t size_small = 10; while (vecset.size() > size_small) { vec_set_type::iterator viter = vecset.begin() + gen(vecset.size()); const int value = *viter; vecset.erase(viter); stdset.erase(value); } } verify(stdset, vecset); for (int i = 0; i != vecset.size(); ++ i) { const int value = *(vecset.begin() + i); if (vecset.lower_bound(value) != vecset.equal_range(value).first) std::cerr << "different lower bound?" << std::endl; if (vecset.upper_bound(value) != vecset.equal_range(value).second) std::cerr << "different upper bound?" << std::endl; } boost::uniform_int<int> gen_int(vecset.front(), vecset.back()); for (int i = 0; i != 1024 * 8; ++ i) { const int value = gen_int(generator); if (vecset.lower_bound(value) != vecset.equal_range(value).first) std::cerr << "different lower bound?" << std::endl; if (vecset.upper_bound(value) != vecset.equal_range(value).second) std::cerr << "different upper bound?" << std::endl; } }
LLVMValueRef gen_expr(compile_t* c, ast_t* ast) { LLVMValueRef ret; bool has_scope = ast_has_scope(ast); bool has_source = codegen_hassource(c); if(has_scope) { codegen_pushscope(c); // Dwarf a new lexical scope, if necessary. if(has_source) dwarf_lexicalscope(&c->dwarf, ast); } switch(ast_id(ast)) { case TK_SEQ: ret = gen_seq(c, ast); break; case TK_FVARREF: case TK_FLETREF: ret = gen_fieldload(c, ast); break; case TK_PARAMREF: ret = gen_param(c, ast); break; case TK_VAR: case TK_LET: ret = gen_localdecl(c, ast); break; case TK_VARREF: case TK_LETREF: ret = gen_localload(c, ast); break; case TK_IF: ret = gen_if(c, ast); break; case TK_WHILE: ret = gen_while(c, ast); break; case TK_REPEAT: ret = gen_repeat(c, ast); break; case TK_TRY: case TK_TRY_NO_CHECK: ret = gen_try(c, ast); break; case TK_MATCH: ret = gen_match(c, ast); break; case TK_CALL: ret = gen_call(c, ast); break; case TK_CONSUME: ret = gen_expr(c, ast_childidx(ast, 1)); break; case TK_RECOVER: ret = gen_expr(c, ast_childidx(ast, 1)); break; case TK_BREAK: ret = gen_break(c, ast); break; case TK_CONTINUE: ret = gen_continue(c, ast); break; case TK_RETURN: ret = gen_return(c, ast); break; case TK_ERROR: ret = gen_error(c, ast); break; case TK_IS: ret = gen_is(c, ast); break; case TK_ISNT: ret = gen_isnt(c, ast); break; case TK_ASSIGN: ret = gen_assign(c, ast); break; case TK_THIS: ret = gen_this(c, ast); break; case TK_TRUE: ret = LLVMConstInt(c->i1, 1, false); break; case TK_FALSE: ret = LLVMConstInt(c->i1, 0, false); break; case TK_INT: ret = gen_int(c, ast); break; case TK_FLOAT: ret = gen_float(c, ast); break; case TK_STRING: ret = gen_string(c, ast); break; case TK_TUPLE: ret = gen_tuple(c, ast); break; case TK_FFICALL: ret = gen_ffi(c, ast); break; case TK_AMP: ret = gen_addressof(c, ast); break; case TK_IDENTITY: ret = gen_identity(c, ast); break; case TK_DONTCARE: ret = GEN_NOVALUE; break; case TK_COMPILER_INTRINSIC: ast_error(ast, "unimplemented compiler intrinsic"); LLVMBuildUnreachable(c->builder); ret = GEN_NOVALUE; break; default: ast_error(ast, "not implemented (codegen unknown)"); return NULL; } if(has_scope) { codegen_popscope(c); if(has_source) dwarf_finish(&c->dwarf); } return ret; }
LLVMValueRef gen_expr(compile_t* c, ast_t* ast) { LLVMValueRef ret; bool has_scope = ast_has_scope(ast); if(has_scope) codegen_pushscope(c, ast); switch(ast_id(ast)) { case TK_SEQ: ret = gen_seq(c, ast); break; case TK_FVARREF: case TK_FLETREF: ret = gen_fieldload(c, ast); break; case TK_EMBEDREF: ret = gen_fieldptr(c, ast); break; case TK_PARAMREF: ret = gen_param(c, ast); break; case TK_VAR: case TK_LET: case TK_MATCH_CAPTURE: ret = gen_localdecl(c, ast); break; case TK_VARREF: case TK_LETREF: ret = gen_localload(c, ast); break; case TK_IF: ret = gen_if(c, ast); break; case TK_WHILE: ret = gen_while(c, ast); break; case TK_REPEAT: ret = gen_repeat(c, ast); break; case TK_TRY: case TK_TRY_NO_CHECK: ret = gen_try(c, ast); break; case TK_MATCH: ret = gen_match(c, ast); break; case TK_CALL: ret = gen_call(c, ast); break; case TK_CONSUME: ret = gen_expr(c, ast_childidx(ast, 1)); break; case TK_RECOVER: ret = gen_expr(c, ast_childidx(ast, 1)); break; case TK_BREAK: ret = gen_break(c, ast); break; case TK_CONTINUE: ret = gen_continue(c, ast); break; case TK_RETURN: ret = gen_return(c, ast); break; case TK_ERROR: ret = gen_error(c, ast); break; case TK_IS: ret = gen_is(c, ast); break; case TK_ISNT: ret = gen_isnt(c, ast); break; case TK_ASSIGN: ret = gen_assign(c, ast); break; case TK_THIS: ret = gen_this(c, ast); break; case TK_TRUE: ret = LLVMConstInt(c->i1, 1, false); break; case TK_FALSE: ret = LLVMConstInt(c->i1, 0, false); break; case TK_INT: ret = gen_int(c, ast); break; case TK_FLOAT: ret = gen_float(c, ast); break; case TK_STRING: ret = gen_string(c, ast); break; case TK_TUPLE: ret = gen_tuple(c, ast); break; case TK_FFICALL: ret = gen_ffi(c, ast); break; case TK_ADDRESS: ret = gen_addressof(c, ast); break; case TK_DIGESTOF: ret = gen_digestof(c, ast); break; case TK_DONTCARE: ret = GEN_NOVALUE; break; case TK_COMPILE_INTRINSIC: ast_error(c->opt->check.errors, ast, "unimplemented compile intrinsic"); return NULL; case TK_COMPILE_ERROR: { ast_t* reason_seq = ast_child(ast); ast_t* reason = ast_child(reason_seq); ast_error(c->opt->check.errors, ast, "compile error: %s", ast_name(reason)); return NULL; } default: ast_error(c->opt->check.errors, ast, "not implemented (codegen unknown)"); return NULL; } if(has_scope) codegen_popscope(c); return ret; }
std::string gen_indexed(std::string const& sVar, int iLiteralIndex) { return sVar + "[" + gen_int(iLiteralIndex) + "]"; }