Esempio n. 1
0
void test_bracket_matching_one_type()
{
	char s0[] = "";
	char s1[] = "()";
	char s2[] = "())";
	char s3[] = "(()";
	char s4[] = "((()())())";	
	bool result;

	result = match_brackets(s0);
	
	if (!result)
		throw part2_tester_exception(__func__,__LINE__);

	result = match_brackets(s1);
	if (!result)
		throw part2_tester_exception(__func__,__LINE__);
	
	result = match_brackets(s2);
	//cout<< result << endl;
	if (result)
		throw part2_tester_exception(__func__,__LINE__);
	
	result = match_brackets(s3);
	if (result)
		throw part2_tester_exception(__func__,__LINE__);	
	
	result = match_brackets(s4);
	if (!result)
		throw part2_tester_exception(__func__,__LINE__);
		
	cout << __func__ << " passed." << endl;	
}
Esempio n. 2
0
File: stack.c Progetto: v27u27/stack
/* Story 5
 * As a programmer, I want to implement bracket matching to see how it
 * works.
 */
void test_match_brackets(char *brackets)
{
  int mismatch = 0;
  if ((mismatch = match_brackets(brackets)) < 0) {
    printf("%s\n", brackets);
    printf("Brackets match\n");
    fflush(stdout);
  } else {
    printf("%s\n", brackets);
    fflush(stdout);
    for (int i = 0; i < mismatch; i++) {
      printf(" ");
    }
    printf("^ mismatch\n");
    fflush(stdout);
  }
}
Esempio n. 3
0
void test_bracket_matching_multiple_types()
{
	char s0[] = "{}<>()";
	char s1[] = "{([])[]}";
	char s2[] = "({}(<{{}}>))";
	char s3[] = "(({[][]})([]{}))";
	char s4[] = "(({()()}())";	
	char s5[] = "((])";	
	char s6[] = "]";	
	
	bool result;
	
	result = match_brackets(s0);
	if (!result)
		throw part2_tester_exception(__func__,__LINE__);

	result = match_brackets(s1);
	if (!result)
		throw part2_tester_exception(__func__,__LINE__);
	
	result = match_brackets(s2);
	if (!result)
		throw part2_tester_exception(__func__,__LINE__);
	
	result = match_brackets(s3);
	if (!result)
		throw part2_tester_exception(__func__,__LINE__);	
	
	result = match_brackets(s4);
	if (result)
		throw part2_tester_exception(__func__,__LINE__);

	result = match_brackets(s5);
	if (result)
		throw part2_tester_exception(__func__,__LINE__);	
	
	result = match_brackets(s6);
	if (result)
		throw part2_tester_exception(__func__,__LINE__);
				
	cout << __func__ << " passed." << endl;	
}
Esempio n. 4
0
File: ai.c Progetto: maxikov/megai
int main()
{
	int *arr_seq, *stack, *match_arr, *tape, tape_len, tape_pointer, cmd_pointer, machine_output, *desired_output;
	int i, seq_len, iters, desired_out_len, j;
	char *program;
	int desired_output_st[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34};
	//int desired_output_st[] = {3, 6, 9, 12, 15};


	tape_len = (MAXITERS+10)*2; //just in case

	tape = (int*)calloc(tape_len, sizeof(int));

	desired_out_len = sizeof(desired_output_st)/sizeof(int);
	desired_output = (int*)malloc(desired_out_len*sizeof(int));
	for(i = 0; i < desired_out_len; i++)
	{
		desired_output[i] = desired_output_st[i];
		//printf("%d - %d\n", i, desired_output[i]);
	}

	for(seq_len = 1; seq_len <= 8; seq_len++)
	{
		printf("Testing programs of length = %d\n", seq_len);
		arr_seq = (int*)calloc(seq_len, sizeof(int));
		program = (char*)malloc((seq_len+1)*sizeof(char));
		stack = (int*)calloc(seq_len, sizeof(int));
		match_arr = (int*)calloc(seq_len, sizeof(int));
		do
		{
			arr_seq_to_program(arr_seq, program, seq_len);
			if (validate_and_optimize(program, seq_len))
			{
				program[seq_len] = '\0';
				match_brackets(program, match_arr, stack, seq_len);
				//printf("%s\n", program);
				for(j = 0; j < tape_len; j++)
					tape[j] = 0;
				tape_pointer = tape_len/2;
				cmd_pointer = 0;
				j = 0;
				for(iters = 0; iters <= MAXITERS; iters++)
				{
					switch (machine_next_step(program, tape, match_arr, &cmd_pointer, &tape_pointer, &machine_output, seq_len))
					{
						case 0:
							goto machine_loop_exit; //f**k
						case 1:
							//printf("M:%d D[%d]:%d; ", machine_output, j, desired_output[j]);
							if (machine_output != desired_output[j])
							{
								//printf("\nFail\n");
								goto machine_loop_exit;
							}
							j++;
							if (j >= desired_out_len)
							{
								printf("Success!!!\n");
								printf("Desired output: ");
								for(j = 0; j < desired_out_len; j++)
									printf("%d ", desired_output[j]);
								printf("\nMachine: %s\n", program);
								goto main_loop_exit;
							}
							
						case 2:
							;
					}

				}
				machine_loop_exit:
				;
				//printf("\n\n");
				
			}
		} while(next_arr_seq(arr_seq, seq_len, MAXVAL));
		free(arr_seq);
		free(program);
		free(stack);
		free(match_arr);
	}
	main_loop_exit:
	free(desired_output);
	free(tape);	
	return 0;
}