Geek-Side

Resource > 3_3

/**
 * 演習問題 3.3
 **/

#include <CUnit/CUnit.h>
#include <CUnit/Console.h>

#include <stdio.h>
#define MAX_SIZE 100

void expand (char s1[], char s2[]) {
  int i, j, k, sign, start_or_end, begin;
  start_or_end = sign = j = 0;
  for (i = 0; i <  strlen(s1); i++) {

    if (i == 0 || i == strlen(s1) - 1) {
      start_or_end = 1;
    } else {
      start_or_end = 0;
    }

    if (s1[i] == '-') {
      if (start_or_end) {
	s2[j++] = '-';
      } else {
	sign = 1;
      }
    } else {
      if (sign) {
	for (k = 1; k <= (s1[i] - begin); k++) {
	  s2[j++] = begin + k;
	}
	sign = 0;
      } else {
	s2[j++] = s1[i];
      }
      begin = s1[i];
    }
  }
}


void testnum(void) {
  char result[MAX_SIZE] = "123456789";
  char before[MAX_SIZE] = "1-9";
  char after[MAX_SIZE] = "";
  expand(before, after);
  CU_ASSERT_STRING_EQUAL(after, result);

  char result2[MAX_SIZE] = "12345678912345";
  char before2[MAX_SIZE] = "1-91-5";
  char after2[MAX_SIZE] = "";
  expand(before2, after2);
  CU_ASSERT_STRING_EQUAL(after2, result2);

  char result3[MAX_SIZE] = "123456789";
  char before3[MAX_SIZE] = "1-5-9";
  char after3[MAX_SIZE] = "";
  expand(before3, after3);
  CU_ASSERT_STRING_EQUAL(after3, result3);

}

void testalpha(void) {
  char result[MAX_SIZE] = "abcdefghijklmnopqrstuvwxyz";
  char before[MAX_SIZE] = "a-z";
  char after[MAX_SIZE] = "";
  expand(before, after);
  CU_ASSERT_STRING_EQUAL(after, result);

  char result2[MAX_SIZE] = "abcdefghijklmnopqrstuvwxyzmnopqrstuvwxyz";
  char before2[MAX_SIZE] = "a-zm-z";
  char after2[MAX_SIZE] = "";
  expand(before2, after2);
  CU_ASSERT_STRING_EQUAL(after2, result2);

  char result3[MAX_SIZE] = "abcdefghijklmnopqrstuvwxyz";
  char before3[MAX_SIZE] = "a-m-z";
  char after3[MAX_SIZE] = "";
  expand(before3, after3);
  CU_ASSERT_STRING_EQUAL(after3, result3);

  char result4[MAX_SIZE] = "z";
  char before4[MAX_SIZE] = "z-a";
  char after4[MAX_SIZE] = "";
  expand(before4, after4);
  CU_ASSERT_STRING_EQUAL(after4, result4);

}

void testEmpty(void) {
  char result[MAX_SIZE] = "";
  char before[MAX_SIZE] = "";
  char after[MAX_SIZE] = "";
  expand(before, after);
  CU_ASSERT_STRING_EQUAL(after, result);
}

void testhifun(void) {
  char result[MAX_SIZE] = "-abcdefghijklmnopqrstuvwxyz-";
  char before[MAX_SIZE] = "-a-z-";
  char after[MAX_SIZE] = "";
  expand(before, after);
  CU_ASSERT_STRING_EQUAL(after, result);

  char result2[MAX_SIZE] = "abcdefghijklmnopqrstuvwxyzmnopqrstuvwxyz";
  char before2[MAX_SIZE] = "a-zm-z";
  char after2[MAX_SIZE] = "";
  expand(before2, after2);
  CU_ASSERT_STRING_EQUAL(after2, result2);
}


int main(void) {
	CU_pSuite testSuite;
	CU_initialize_registry();
	testSuite = CU_add_suite("3_3TestSuite", NULL, NULL);
	CU_add_test(testSuite, "Numbertest", testnum);
	CU_add_test(testSuite, "Alphabettest", testalpha);
	CU_add_test(testSuite, "Hifuntest", testhifun);
	CU_add_test(testSuite, "Emptytest", testEmpty);
	CU_console_run_tests();
	CU_cleanup_registry();

	return 0;
}