示例#1
0
int test(){
  int ret = 0;
  write_cstr(1, ">>> test >>>\n");
  struct arr *a = arr_alloc(4);
  arr_insert(a, 0, "this", 4);
  ret |= test_print_eq(a, "this", "arr 0");
  arr_insert(a, a->c, " is", 3);
  test_print_eq(a, "this is", "arr 0.1");
  arr_insert(a, 0, "hi ", 3);
  test_print_eq(a, "hi this is", "arr 0.2");
  arr_insert(a, 0, "this is a big string being added. ", strlen("this is a big string being added. "));
  test_print_eq(a, "this is a big string being added. hi this is", "arr 1");
  struct arr *b = arr_alloc(4);
  arr_insert(b, 0, "starting:", strlen("starting:"));
  test_print_eq(b, "starting:", "arr 2");
  arr_append_int_str(b, 3);
  test_print_eq(b, "starting:3", "arr 3");
  arr_append_int_str(b, 40012);
  ret |= test_print_eq(b, "starting:340012", "arr 4");
  struct arr *c = arr_alloc(4);
  arr_append(c, "h");
  arr_append(c, "i");
  test_print_eq(c, "hi", "arr 5");
  write(1, "\n", 1);
  return ret;
}
示例#2
0
int test_uarr(){
  int ret = 0;
  write_cstr(1, ">>> test_uarr >>>\n");
  int l;

  /* test insert & append */
  struct arr *msg = arr_alloc(16);
  struct uarr *p = uarr_alloc(4, sizeof(int));
  int iarr[] = {1,2,3,4};
  uarr_insert(p, uarr_count(p), &iarr[0], 1);
  uarr_insert(p, uarr_count(p), &iarr[1], 1);
  uarr_append(p, &iarr[2]);
  uarr_append(p, &iarr[3]);
  int *ip = (int *)p->v;
  l = uarr_count(p);
  while(l--){
    arr_append_int_str(msg, *ip);
    arr_insert(msg, msg->c, ",", 1);
    ip++;
  }
  ret |= test_print_eq(msg, "1,2,3,4,", "uarr 1");
  arr_free(msg);
  uarr_free(p);
  write(1, "\n", 1);

  /* test append */
  struct arr *msg2 = arr_alloc(16);
  struct uarr *p2 = uarr_alloc(4, sizeof(int));
  int iarr2[] = {1,2,3,4};
  uarr_append(p2, &iarr2[0]);
  uarr_append(p2, &iarr2[1]);
  uarr_append(p2, &iarr2[2]);
  uarr_append(p2, &iarr2[3]);
  int *ip2 = (int *)p2->v;
  l = uarr_count(p2);
  while(l--){
    arr_append_int_str(msg2, *ip2);
    arr_insert(msg2, msg2->c, ",", 1);
    ip2++;
  }
  ret |= test_print_eq(msg2, "1,2,3,4,", "uarr 2");
  arr_free(msg2);
  uarr_free(p2);
  write(1, "\n", 1);
  ret |= test_uarr_ptr();
  return ret;
}
示例#3
0
int parse_object(const char** input, JzonValue* output, bool root_object, JzonAllocator* allocator)
{
	if (current(input) == '{')
next(input);
	else if (!root_object)
		return -1;

		output->is_object = true;

		// Empty object.
		if (current(input) == '}')
		{
			output->size = 0;
			return 0;
		}

		Array object_values = { 0 };

		while (current(input))
		{
			JzonKeyValuePair* pair = (JzonKeyValuePair*)allocator->allocate(sizeof(JzonKeyValuePair));
			skip_whitespace(input);
			char* key = parse_keyname(input, allocator);
			skip_whitespace(input);

			if (key == NULL || current(input) != ':')
				return -1;

			next(input);
			JzonValue* value = (JzonValue*)allocator->allocate(sizeof(JzonValue));
			memset(value, 0, sizeof(JzonValue));
			int error = parse_value(input, value, allocator);

			if (error != 0)
				return error;

			pair->key = key;
			pair->key_hash = hash_str(key);
			pair->value = value;
			arr_insert(&object_values, pair, find_object_pair_insertion_index((JzonKeyValuePair**)object_values.arr, object_values.size, pair->key_hash), allocator);
			skip_whitespace(input);

			if (current(input) == '}')
			{
				next(input);
				break;
			}
		}

		output->size = object_values.size;
		output->object_values = (JzonKeyValuePair**)object_values.arr;
		return 0;
}
示例#4
0
int test_uarr_ptr(){
  int ret = 0, l, ip;
  struct arr *msg = arr_alloc(16);
  struct uarr *p = uarr_alloc(2, sizeof(char *));
  char *carr[] = {"hi","there","alpha","beta"};
  uarr_append(p, &carr[0]);
  uarr_append(p, &carr[1]);
  uarr_append(p, &carr[2]);
  uarr_append(p, &carr[3]);
  char **cp = (char **)p->v;
  l = uarr_count(p);
  while(l--){
    arr_insert(msg, msg->c, *cp, strlen(*cp));
    arr_insert(msg, msg->c, ",", 1);
    cp++;
  }
  ret |= test_print_eq(msg, "hi,there,alpha,beta,", "uarr ptr 1");
  arr_free(msg);
  uarr_free(p);
  write(1, "\n", 1);
  return ret;
}
示例#5
0
int test_print_eq(struct arr *a, char *s, char *title){
  struct arr *msg = arr_alloc(4);
  int r;
  if((r = str_eq(a->v, s, a->c)) == 0){
    arr_insert(msg, msg->c, ".", 1);
    write(1, msg->v, msg->c);
    return 0;
  }else{
		/*
    arr_append_cstr(msg, "fail - ");
    arr_append_cstr(msg, title);
    arr_append_cstr(msg, ":");
    arr_append_int_str(msg, r);
    arr_append_cstr(msg, ":");
		*/
    write(1, msg->v, msg->c);
    write(1, a->v, a->c);
    return 123;
  }
}
示例#6
0
// 예약
void ma_reserve()
{
	int i;
	
	Member *res = (Member*)malloc(sizeof(Member)); 
	arr_info(&g_arr);
	
	while(1)
	{
		printf("예약매수 입력: ");		scanf("%d",&res->count);
		if(res->count > 0 && res->count < 6)
			break;
		else
			printf("재입력...\n");
	}
	for(i=0; i < res->count; i++)
	{
		printf("좌석 입력 : ");		scanf("%d",&res->seat[i]);
		if(g_arr.data[res->seat[i]-1] != NULL)
		{
			printf("재입력...\n");
			i -= 1;
			continue;
		}
	}
	fflush(stdin);
	printf("전화번호 입력 : ");		gets(res->phone);
	for(i=0; i<5; i++)
	{
		res->number[i]=rand()%10+48;
		if(i==4)
			res->number[i+1]='\0';
	}
	for(i=0; i < res->count; i++)
	{
		arr_insert(&g_arr, res,res->seat[i]);			//<===================================		
	}
	list_insert(&g_list, res);
	printf("예약이 완료 되었습니다..\n");
	printf("예약번호: %s\n",res->number); 
}