Geek-Side

Resource > 3_2

/**
 * 演習問題 3.2
 **/

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

#include <stdio.h>
#define MAX_SIZE 100

void escape (char s[], char t[], int size) {
  int  i, j;

  i = j = 0;
  for (i = 0; i < size; i++) {
    switch (s[i]) {
    case 'n'
      t[j++] = '\';
      t[j++] = 'n';
      break;
    case 't':
      t[j++] = '\';
      t[j++] = 't';
      break;
    default:
      t[j++] = s[i];
      break;
    }
  }
}

void testtab(void) {
  char result[MAX_SIZE] = "hoge\thige\tfuga";
  char before[MAX_SIZE] = "hoge	hige	fuga";
  char after[MAX_SIZE] = "";
  escape(before, after, MAX_SIZE);
  CU_ASSERT_STRING_EQUAL(after, result);

  char result2[MAX_SIZE] = "hoge\t\thige";
  char before2[MAX_SIZE] = "hoge		hige";
  char after2[MAX_SIZE] = "";
  escape(before2, after2, MAX_SIZE);
  CU_ASSERT_STRING_EQUAL(after2, result2);

  char result3[MAX_SIZE] = "\t\t";
  char before3[MAX_SIZE] = "		";
  char after3[MAX_SIZE] = "";
  escape(before3, after3, MAX_SIZE);
  CU_ASSERT_STRING_EQUAL(after3, result3);
}

void testNewLine(void) {
  char result[MAX_SIZE] = "hoge\nhige";
  char before[MAX_SIZE] = "hogenhige";
  char after[MAX_SIZE] = "";
  escape(before, after, MAX_SIZE);
  CU_ASSERT_STRING_EQUAL(after, result);
  
  char result2[MAX_SIZE] = "hoge\n\nhige";
  char before2[MAX_SIZE] = "hogennhige";
  char after2[MAX_SIZE] = "";
  escape(before2, after2, MAX_SIZE);
  CU_ASSERT_STRING_EQUAL(after2, result2);

}

void testEmpty(void) {

  char result[MAX_SIZE] = "";
  char before[MAX_SIZE] = "";
  char after[MAX_SIZE] = "";
  escape(before, after, MAX_SIZE);
  CU_ASSERT_STRING_EQUAL(after, result);
}

int main(void) {
	CU_pSuite testSuite;
	CU_initialize_registry();
	testSuite = CU_add_suite("3_2TestSuite", NULL, NULL);
	CU_add_test(testSuite, "Tabtest", testtab);
	CU_add_test(testSuite, "NewLinetest", testNewLine);
	CU_add_test(testSuite, "Emptytest", testEmpty);
	CU_console_run_tests();
	CU_cleanup_registry();
	


	return 0;
}