void test_deleting_a_nonexistent_order_produces_correct_status() { fh_shr_lkp_tbl_t *table = valid_table(); fh_shr_lkp_ord_t *entry; FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_del(table, valid_key(), &entry), (int)FH_ERR_NOTFOUND); FH_TEST_ASSERT_NULL(entry); FH_TEST_ASSERT_EQUAL(table->count, 0); }
int32_t flip_unfixed_variable(samp_table_t *table, int32_t var) { // double dcost = 0; //dcost seems unnecessary atom_table_t *atom_table = &table->atom_table; cprintf(4, "[flip_unfixed_variable] Flipping variable %"PRId32" to %s\n", var, assigned_true(atom_table->assignment[var]) ? "false" : "true"); assert(valid_table(table)); if (assigned_true(atom_table->assignment[var])) { update_atom_tval(var, v_false, table); } else { update_atom_tval(var, v_true, table); } scan_live_clauses(table); assert(valid_table(table)); return 0; }
void test_deleting_an_existing_order_decrements_count() { fh_shr_lkp_tbl_t *table = valid_table(); fh_shr_lkp_ord_t *entry; FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_add(table, valid_entry(), &entry), (int)FH_OK); FH_TEST_ASSERT_EQUAL(table->count, 1); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_del(table, valid_key(), &entry), (int)FH_OK); FH_TEST_ASSERT_EQUAL(table->count, 0); }
void test_fetch_returns_same_pointer_as_add() { fh_shr_lkp_ord_t *addentry, *getentry; fh_shr_lkp_tbl_t *table = valid_table(); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_add(table, valid_entry(), &addentry), (int)FH_OK); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_get(table, valid_key(), &getentry), (int)FH_OK); FH_TEST_ASSERT_LEQUAL((unsigned long)addentry, (unsigned long)getentry); }
void test_order_is_not_fetchable_after_delete() { fh_shr_lkp_tbl_t *table = valid_table(); fh_shr_lkp_ord_t *entry; FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_add(table, valid_entry(), &entry), (int)FH_OK); FH_TEST_ASSERT_EQUAL(table->count, 1); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_del(table, valid_key(), &entry), (int)FH_OK); FH_TEST_ASSERT_EQUAL(table->count, 0); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_get(table, valid_key(), &entry), (int)FH_ERR_NOTFOUND); }
void test_modified_entry_persists_after_refetching() { fh_shr_lkp_ord_t *tblentry; fh_shr_lkp_tbl_t *table = valid_table(); fh_shr_lkp_ord_t *entry = valid_entry(); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_add(table, entry, &tblentry), (int)FH_OK); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_get(table, valid_key(), &tblentry), (int)FH_OK); tblentry->shares = 200; FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_get(table, valid_key(), &tblentry), (int)FH_OK); FH_TEST_ASSERT_LEQUAL(tblentry->order_no, entry->order_no); FH_TEST_ASSERT_LEQUAL(tblentry->price, entry->price); FH_TEST_ASSERT_EQUAL(tblentry->shares, 200); FH_TEST_ASSERT_EQUAL(tblentry->buy_sell_ind, entry->buy_sell_ind); FH_TEST_ASSERT_STREQUAL(tblentry->stock, entry->stock); FH_TEST_ASSERT_EQUAL(table->count, 1); }
fh_shr_cfg_tbl_t *valid_config() { static fh_shr_cfg_tbl_t config = { .name = "dummy_config", .enabled = 1, .size = 100 }; return &config; } fh_shr_lkp_tbl_t *valid_table() { static fh_shr_lkp_tbl_t table; fh_shr_lkp_ord_init(valid_config(), &table); return &table; } fh_shr_lkp_ord_t *valid_entry() { static fh_shr_lkp_ord_t entry = { .order_no = 1, .price = 1005000, .shares = 100, .buy_sell_ind = 'B', .stock = "AAPL", .sym_entry = NULL, .context = NULL }; return &entry; } fh_shr_lkp_ord_key_t *valid_key() { static fh_shr_lkp_ord_key_t key = { .order_no = 1 }; return &key; } void test_count_responds_to_entry_insertion() { fh_shr_lkp_tbl_t *table = valid_table(); fh_shr_lkp_ord_t *tblentry; FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_add(table, valid_entry(), &tblentry), (int)FH_OK); FH_TEST_ASSERT_EQUAL(table->count, 1); } void test_get_after_insertion_yields_correct_values() { fh_shr_lkp_ord_t *tblentry; fh_shr_lkp_tbl_t *table = valid_table(); fh_shr_lkp_ord_t *entry = valid_entry(); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_add(table, entry, &tblentry), (int)FH_OK); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_get(table, valid_key(), &tblentry), (int)FH_OK); FH_TEST_ASSERT_LEQUAL(tblentry->order_no, entry->order_no); FH_TEST_ASSERT_LEQUAL(tblentry->price, entry->price); FH_TEST_ASSERT_EQUAL(tblentry->shares, entry->shares); FH_TEST_ASSERT_EQUAL(tblentry->buy_sell_ind, entry->buy_sell_ind); FH_TEST_ASSERT_STREQUAL(tblentry->stock, entry->stock); FH_TEST_ASSERT_EQUAL(table->count, 1); } void test_retrieving_entry_with_freed_key_works() { fh_shr_lkp_ord_key_t *key; fh_shr_lkp_ord_t *entry; fh_shr_lkp_tbl_t *table = valid_table(); key = (fh_shr_lkp_ord_key_t *)malloc(sizeof(fh_shr_lkp_ord_key_t)); memcpy(key, valid_key(), sizeof(fh_shr_lkp_ord_key_t)); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_add(table, valid_entry(), &entry), (int)FH_OK); free(key); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_get(table, valid_key(), &entry), (int)FH_OK); FH_TEST_ASSERT_EQUAL(table->count, 1); }
/* * [lazy only] Choose a random atom for simulated annealing step in sample SAT. * The lazy version of choose_unfixed_variable. First choose a random atom, * regardless whether its value is fixed or not (because we can calculate the * total number of atoms and it is convenient to randomly choose one from * them). If its value is fixed, we skip this flip using the following * statement (return 0). */ int32_t choose_random_atom(samp_table_t *table) { uint32_t i, atom_num, anum; int32_t card, all_card, acard, pcard, predicate; pred_tbl_t *pred_tbl = &table->pred_table.pred_tbl; // Indirect preds atom_table_t *atom_table = &table->atom_table; sort_table_t *sort_table = &table->sort_table; pred_entry_t *pred_entry; assert(valid_table(table)); /* Get the number of possible indirect atoms */ all_card = all_atoms_cardinality(pred_tbl, sort_table); //atom_num = random_uint(all_card); atom_num = genrand_uint(all_card); predicate = 1; /* Skip past true */ acard = 0; while (true) { /* determine the predicate */ assert(predicate <= pred_tbl->num_preds); pcard = pred_cardinality(pred_tbl, sort_table, predicate); if (acard + pcard > atom_num) { break; } acard += pcard; predicate++; } assert(pred_cardinality(pred_tbl, sort_table, predicate) != 0); /* gives the position of atom within predicate */ anum = atom_num - acard; /* * Now calculate the arguments. We represent the arguments in * little-endian form */ pred_entry = &pred_tbl->entries[predicate]; int32_t *signature = pred_entry->signature; int32_t arity = pred_entry->arity; atom_buffer_resize(arity); int32_t constant; samp_atom_t *atom = (samp_atom_t *) atom_buffer.data; /* Build atom from atom_num by successive division */ atom->pred = predicate; for (i = 0; i < arity; i++) { card = sort_table->entries[signature[i]].cardinality; constant = anum % card; anum = anum / card; sort_entry_t *sort_entry = &sort_table->entries[signature[i]]; if (sort_entry->constants == NULL) { /* Must be an integer */ if (sort_entry->ints == NULL) { atom->args[i] = sort_entry->lower_bound + constant; } else { atom->args[i] = sort_entry->ints[constant]; } } else { atom->args[i] = sort_entry->constants[constant]; /* Quick typecheck */ assert(const_sort_index(atom->args[i], &table->const_table) == signature[i]); } } assert(valid_table(table)); array_hmap_pair_t *atom_map; atom_map = array_size_hmap_find(&atom_table->atom_var_hash, arity + 1, (int32_t *) atom); int32_t atom_index; if (atom_map == NULL) { /* need to activate atom */ atom_index = add_internal_atom(table, atom, false); atom_map = array_size_hmap_find(&atom_table->atom_var_hash, arity + 1, (int32_t *) atom); assert(atom_map != NULL); activate_atom(table, atom_index); } else { atom_index = atom_map->val; } return atom_index; }