void test_move_card_from_non_stack_empty_stack_to_non_stack_empty_stack() { struct stack *origin, *destination; struct card *card[6]; for (int i = 0; i < 6; i++) { card_malloc(&card[i]); card_init(card[i]); card_set(card[i], TWO + i, i % 5, i % 2, 99, 99); } stack_malloc(&origin); stack_malloc(&destination); stack_init(origin); stack_init(destination); for (int i = 0; i < 3; i++) { stack_push(&origin, card[i]); } for (int i = 3; i < 6; i++) { stack_push(&destination, card[i]); } move_card(&origin, &destination); assert(stack_length(origin) == 2); assert(stack_length(destination) == 4); assert(cards_equal(destination->card, card[2])); assert(cards_equal(destination->next->card, card[5])); stack_free(origin); stack_free(destination); }
void test_valid_move_from_stock_to_maneuvre_stacks() { struct stack *stock, *maneuvre_stacks[7]; stack_malloc(&stock); stack_init(stock); card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X); for (int i = 0; i < 7; i++) { stack_malloc(&maneuvre_stacks[i]); stack_init(maneuvre_stacks[i]); } card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X); card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X); card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X); card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X); card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X); card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X); card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X); for (int i = 0; i < 7; i++) { assert(!valid_move(stock, maneuvre_stacks[i])); } stack_free(stock); for (int i = 0; i < 7; i++) { stack_free(maneuvre_stacks[i]); } }
void test_move_card_from_stack_empty_stack_to_non_stack_empty_stack() { struct stack *origin, *destination, *new_origin, *new_destination, *origin_duplicate, *destination_duplicate; struct card *card; card_malloc(&card); card_init(card); card_set(card, ACE, SPADES, EXPOSED, 0, 0); stack_malloc(&origin); stack_malloc(&destination); stack_init(origin); stack_init(destination); new_origin = origin; new_destination = destination; stack_push(&new_destination, card); origin_duplicate = stack_dup(origin); destination_duplicate = stack_dup(destination); move_card(&new_origin, &new_destination); assert(origin == new_origin); assert(stacks_equal(origin, origin_duplicate)); assert(destination == new_destination); assert(stacks_equal(destination, destination_duplicate)); stack_free(origin); stack_free(destination); }
void test_valid_move_from_maneuvre_stack_to_foundation_stacks() { struct stack *foundation_stacks[4]; struct stack *maneuvre_stacks[7]; for (int i = 0; i < 4; i++) { stack_malloc(&foundation_stacks[i]); stack_init(foundation_stacks[i]); } card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X); card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X); card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X); card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X); for (int i = 0; i < 7; i++) { stack_malloc(&maneuvre_stacks[i]); stack_init(maneuvre_stacks[i]); } card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X); card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X); card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X); card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X); card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X); card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X); card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X); for (int i = 0; i < 7; i++) { for (int j = 0; j < 4; j++) { assert(valid_move(maneuvre_stacks[i], foundation_stacks[j])); } } for (int i = 0; i < 4; i++) { stack_free(foundation_stacks[i]); } for (int i = 0; i < 7; i++) { stack_free(maneuvre_stacks[i]); } }
void test_valid_move_from_maneuvre_stack_to_waste_pile() { struct stack *waste_pile, *maneuvre_stacks[7]; stack_malloc(&waste_pile); stack_init(waste_pile); card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X); for (int i = 0; i < 7; i++) { stack_malloc(&maneuvre_stacks[i]); stack_init(maneuvre_stacks[i]); } card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X); card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X); card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X); card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X); card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X); card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X); card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X); for (int i = 0; i < 7; i++) { assert(!valid_move(maneuvre_stacks[i], waste_pile)); } stack_free(waste_pile); for (int i = 0; i < 7; i++) { stack_free(maneuvre_stacks[i]); } }
void test_move_card_should_not_change_stack_empty_stack_coordinates() { struct stack *origin, *destination; struct card *card[2]; card_malloc(&card[0]); card_malloc(&card[1]); card_init(card[0]); card_init(card[1]); card_set(card[0], ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X); card_set(card[1], KING, HEARTS, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X); stack_malloc(&origin); stack_malloc(&destination); stack_init(origin); stack_init(destination); stack_push(&origin, card[0]); stack_push(&destination, card[1]); move_card(&origin, &destination); assert(origin->card->frame->begin_y == MANEUVRE_BEGIN_Y); assert(origin->card->frame->begin_x == MANEUVRE_0_BEGIN_X); stack_free(origin); stack_free(destination); }
void test_valid_move_from_waste_pile_to_stock() { struct stack *stock, *waste_pile; stack_malloc(&stock); stack_malloc(&waste_pile); stack_init(stock); stack_init(waste_pile); card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X); card_set(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X); assert(!valid_move(waste_pile, stock)); stack_free(stock); stack_free(waste_pile); }
void test_valid_move_from_maneuvre_stack_to_maneuvre_stacks() { struct stack *maneuvre_stacks[7]; for (int i = 0; i < 7; i++) { stack_malloc(&maneuvre_stacks[i]); stack_init(maneuvre_stacks[i]); } card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X); card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X); card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X); card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X); card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X); card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X); card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X); for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { if (i == j) { assert(!valid_move(maneuvre_stacks[i], maneuvre_stacks[j])); } else { assert(valid_move(maneuvre_stacks[i], maneuvre_stacks[j])); } } } for (int i = 0; i < 7; i++) { stack_free(maneuvre_stacks[i]); } }
void test_valid_move_from_waste_pile_to_waste_pile() { struct stack *waste_pile_0, *waste_pile_1; stack_malloc(&waste_pile_0); stack_malloc(&waste_pile_1); stack_init(waste_pile_0); stack_init(waste_pile_1); card_set(waste_pile_0->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X); card_set(waste_pile_1->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X); assert(!valid_move(waste_pile_0, waste_pile_0)); assert(!valid_move(waste_pile_0, waste_pile_1)); assert(!valid_move(waste_pile_1, waste_pile_0)); assert(!valid_move(waste_pile_1, waste_pile_1)); stack_free(waste_pile_0); stack_free(waste_pile_1); }
void test_valid_move_from_stock_to_stock() { struct stack *stock_0, *stock_1; stack_malloc(&stock_0); stack_malloc(&stock_1); stack_init(stock_0); stack_init(stock_1); card_set(stock_0->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X); card_set(stock_1->card, KING, HEARTS, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X); assert(!valid_move(stock_0, stock_0)); assert(!valid_move(stock_0, stock_1)); assert(!valid_move(stock_1, stock_0)); assert(!valid_move(stock_1, stock_1)); stack_free(stock_0); stack_free(stock_1); }
const struct bb_group * get_F2xqE_group(void ** pt_E, GEN a2, GEN a6, GEN T) { struct _F2xqE *e = (struct _F2xqE *) stack_malloc(sizeof(struct _F2xqE)); e->a2 = a2; e->a6 = a6; e->T = T; *pt_E = (void *) e; return &F2xqE_group; }
static char * what_readline(void) { #ifdef READLINE const char *v = READLINE; char *s = stack_malloc(3 + strlen(v) + 8); (void)sprintf(s, "v%s %s", v, GP_DATA->use_readline? "enabled": "disabled"); return s; #else return (char*)"not compiled in"; #endif }
static char * what_cc(void) { char *s; #ifdef GCC_VERSION # ifdef __cplusplus # define Format "(C++) %s" # else # define Format "%s" # endif s = stack_malloc(6 + strlen(GCC_VERSION) + 1); (void)sprintf(s, Format, GCC_VERSION); #else # ifdef _MSC_VER s = stack_malloc(32); (void)sprintf(s, "MSVC-%i", _MSC_VER); # else s = NULL; # endif #endif return s; }
/* strlen(prompt) + 28 chars */ static const char * color_prompt(const char *prompt) { long n = strlen(prompt); char *t = stack_malloc(n + 28), *s = t; *s = 0; /* escape sequences bug readline, so use special bracing (if available) */ brace_color(s, c_PROMPT, 0); s += strlen(s); strncpy(s, prompt, n); s += n; *s = 0; brace_color(s, c_INPUT, 1); return t; }
void test_valid_move_from_stock_to_foundation_stacks() { struct stack *stock, *foundation_stacks[4]; stack_malloc(&stock); stack_init(stock); card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X); for (int i = 0; i < 4; i++) { stack_malloc(&foundation_stacks[i]); stack_init(foundation_stacks[i]); } card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X); card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X); card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X); card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X); for (int i = 0; i < 4; i++) { assert(!valid_move(stock, foundation_stacks[i])); } stack_free(stock); for (int i = 0; i < 4; i++) { stack_free(foundation_stacks[i]); } }
void test_valid_move_from_foundation_stack_to_waste_pile() { struct stack *waste_pile, *foundation_stacks[4]; stack_malloc(&waste_pile); stack_init(waste_pile); card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X); for (int i = 0; i < 4; i++) { stack_malloc(&foundation_stacks[i]); stack_init(foundation_stacks[i]); } card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X); card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X); card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X); card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X); for (int i = 0; i < 4; i++) { assert(!valid_move(foundation_stacks[i], waste_pile)); } stack_free(waste_pile); for (int i = 0; i < 4; i++) { stack_free(foundation_stacks[i]); } }
void pari_center(const char *s) { pari_sp av = avma; long i, l = strlen(s), pad = term_width() - l; char *buf, *u; if (pad<0) pad=0; else pad >>= 1; u = buf = stack_malloc(l + pad + 2); for (i=0; i<pad; i++) *u++ = ' '; while (*s) *u++ = *s++; *u++ = '\n'; *u = 0; pari_puts(buf); avma = av; }
void test_move_card_from_stack_empty_stack_to_stack_empty_stack() { struct stack *origin, *destination, *new_origin, *new_destination, *origin_duplicate, *destination_duplicate; stack_malloc(&origin); stack_malloc(&destination); stack_init(origin); stack_init(destination); new_origin = origin; new_destination = destination; origin_duplicate = stack_dup(origin); destination_duplicate = stack_dup(destination); move_card(&new_origin, &new_destination); assert(origin == new_origin); assert(stacks_equal(origin, origin_duplicate)); assert(destination == new_destination); assert(stacks_equal(destination, destination_duplicate)); stack_free(origin); stack_free(destination); }
/** * reads input from given stdin, and compares the content of the string * @param read * */ void command_read(char *read) { int id = -1; person *pers = NULL; char name[BUF_LEN]; /*static allocation of username*/ memset(name, 0, sizeof (name)); if (strcmp(read, "exit") == 0) { puts("to exit"); free(command_line); if (!is_empty()) { free_memory(); } exit(1); } else if (strstr(read, "add") != NULL) { if (sscanf(read, "add %d %[^\t\n]", &id, name) == 2) { pers = (person *) stack_malloc(sizeof (person)); pers->id = id; strcpy(pers->name, name); enqueue(pers); } } else if (strcmp(read, "print") == 0) { print_q(); } else if (strstr(read, "find") != NULL) { if (sscanf(read, "find %s", name) == 1) { if (search(name) > -1) { printf("found person: %s\n", name); } else { puts("person not found, try to type in full name"); } } } else if (strstr(read, "remove") != NULL) { if (sscanf(read, "remove %s", name) == 1) { if (remove_node(name) > -1) { printf("removed: %s\n", name); } else { puts("person not found, try to type in full name or it doesn't exists"); } } } }
int main(int argc,char** argv) { int i,m,a,b; long long k; char_stack* s; char* ans; while(scanf("%d",&m)!=EOF) { if(m==0) { break; } scanf("%d%d",&a,&b); k=a+b; s=stack_malloc(); if(k) { while(k) { stack_push(s,(k%m)+'0'); k/=m; } } else/* k=0应该直接放一个0*/ { stack_push(s,'0'); } ans=(char*)malloc((s->top+2)*sizeof(char)); i=0; while(!stack_empty(s)) { ans[i++]=stack_pop(s); } ans[i]=0; printf("%s\n",ans); stack_free(s); free(ans); } return 0; }
/********************************************************************/ static int is_interactive(void) { return cb_pari_is_interactive? cb_pari_is_interactive(): 0; } static const char esc = (0x1f & '['); /* C-[ = escape */ static char * strip_prompt(const char *s) { long l = strlen(s); char *t, *t0 = stack_malloc(l+1); t = t0; for (; *s; s++) { /* RL_PROMPT_START_IGNORE / RL_PROMPT_END_IGNORE */ if (*s == 1 || *s == 2) continue; if (*s == esc) /* skip ANSI color escape sequence */ { while (*++s != 'm') if (!*s) goto end; continue; } *t = *s; t++; } end: *t = 0; return t0; }
void test_valid_move_from_foundation_stack_to_foundation_stacks() { struct stack *foundation_stacks[4]; for (int i = 0; i < 4; i++) { stack_malloc(&foundation_stacks[i]); stack_init(foundation_stacks[i]); } card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X); card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X); card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X); card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (i == j) { assert(!valid_move(foundation_stacks[i], foundation_stacks[j])); } else { assert(valid_move(foundation_stacks[i], foundation_stacks[j])); } } } for (int i = 0; i < 4; i++) { stack_free(foundation_stacks[i]); } }