OSDN Git Service

* Added test code of the qs_add_attr() function.
authorkonn <konn@1a406e8e-add9-4483-a2c8-d8cac5b7c224>
Thu, 1 May 2008 06:56:38 +0000 (06:56 +0000)
committerkonn <konn@1a406e8e-add9-4483-a2c8-d8cac5b7c224>
Thu, 1 May 2008 06:56:38 +0000 (06:56 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/modchxj/mod_chxj/branches/RELEASE_0_12_0@2591 1a406e8e-add9-4483-a2c8-d8cac5b7c224

include/qs_log.h
src/qs_log.c
src/qs_parse_attr.c
src/qs_parse_string.c
src/qs_parse_tag.c
test/qs_parse_tag/Makefile [new file with mode: 0644]
test/qs_parse_tag/test_qs_parse_tag.c [new file with mode: 0644]

index 5a0536c..63640bc 100644 (file)
 #define QX_LOGGER_DEBUG_INT(x,y)  qs_log_int(doc,QX_LOG_DEBUG,QX_LOGMARK,(x),(y))
 
 extern void qs_log(
-  Doc*          doc,
+  Doc           *doc,
   int           log_level,
-  const char*   f, 
+  const char    *f, 
   int           l,
-  char*         msg);
+  char          *msg);
 
 extern void qs_log_int(
-  Doc*          doc,
+  Doc           *doc,
   int           log_level,
-  const char*   f, 
+  const char    *f, 
   int           l, 
-  char*         msg,
+  char          *msg,
   int           val);
 
 #ifdef UNUSED
index dcef9e7..3ea4284 100644 (file)
@@ -79,11 +79,12 @@ qs_log(Doc *doc, int log_level,const char *f, int l, char *msg)
     }
     while(0);
 #endif
-    if (doc->r) {
+    if (doc && doc->r) {
       chxj_log_rerror(f,l, APLOG_CRIT, 0, doc->r, msg);
     }
-    qs_all_free(doc, QX_LOGMARK);
-    _exit(0);
+    else {
+      fprintf(stderr, "%s\n", msg);
+    }
   }
 }
 
index 01f0218..1428a72 100644 (file)
@@ -169,10 +169,19 @@ qs_new_attr(Doc *doc)
 {
   Attr *attr;
 
-  attr = (Attr *)apr_palloc(doc->pool,sizeof(Attr));
+  if (!doc) {
+    QX_LOGGER_FATAL("runtime exception: qs_new_attr(): doc is null");
+    return NULL;
+  }
+  if (!doc->pool) {
+    QX_LOGGER_FATAL("runtime exception: qs_new_attr(): doc->pool is null");
+    return NULL;
+  }
 
+  attr = (Attr *)apr_palloc(doc->pool,sizeof(Attr));
   if (attr == NULL) {
     QX_LOGGER_FATAL("Out Of Memory");
+    return NULL;
   }
 
   attr->next   = NULL;
index 6e668c6..49ee4ac 100644 (file)
@@ -100,8 +100,11 @@ qs_parse_string(Doc *doc, const char *src, int srclen)
 
     if ((unsigned char)'<' == src[ii]) {
       int endpoint = s_cut_tag(&src[ii], srclen - ii);
-      Node *node   = NULL;
-      node = qs_parse_tag(doc, &src[ii], endpoint);
+      Node *node   = qs_parse_tag(doc, &src[ii], endpoint);
+      if (! node) {
+        QX_LOGGER_FATAL("runtime exception: qs_parse_string(): Out of memory.");
+        return doc->root_node;
+      }
       ii += endpoint;
 
       if (node->name[0] != '?') break; 
@@ -193,8 +196,11 @@ qs_parse_string(Doc *doc, const char *src, int srclen)
     }
     if ((unsigned char)'<' == src[ii]) {
       int endpoint = s_cut_tag(&src[ii], srclen - ii);
-      Node *node   = NULL;
-      node = qs_parse_tag(doc, &src[ii], endpoint);
+      Node *node   = qs_parse_tag(doc, &src[ii], endpoint);
+      if (! node) {
+        QX_LOGGER_FATAL("runtime exception: qs_parse_string(): Out of memory.");
+        return doc->root_node;
+      }
       node->line = nl_cnt;
 
       ii += endpoint;
@@ -290,6 +296,10 @@ qs_parse_string(Doc *doc, const char *src, int srclen)
       /* TEXT */
       int endpoint = s_cut_text(&src[ii], srclen - ii, script_flag);
       Node *node = qs_new_tag(doc);
+      if (! node) {
+        QX_LOGGER_DEBUG("runtime exception: qs_parse_string(): Out of memory");        
+        return doc->root_node;
+      }
       node->value = (char *)apr_palloc(doc->pool,endpoint+1);
       node->name  = (char *)apr_palloc(doc->pool,4+1);
       node->otext = (char *)apr_palloc(doc->pool,endpoint+1);
index de7a178..d3fdb42 100644 (file)
@@ -47,6 +47,10 @@ qs_parse_tag(Doc *doc, const char *s, int len)
   tag_name = (char *)s_get_tag_name(doc, ++s, --ll);
 
   node = (Node *)qs_new_tag(doc);
+  if (! node) {
+    QX_LOGGER_DEBUG("runtime exception: qs_parse_tag(): Out of memory.");
+    return NULL;
+  }
   node->name = tag_name;
   node->otext = apr_palloc(doc->pool,len+2);
   memset(node->otext, 0, len+2);
@@ -122,7 +126,18 @@ s_get_tag_name(Doc *doc, const char *s, int len)
 Node *
 qs_new_tag(Doc *doc) 
 {
-  Node *node      = (Node *)apr_palloc(doc->pool, sizeof(Node));
+  Node *node;
+
+  if (! doc) {
+    QX_LOGGER_FATAL("runtime exception: qs_new_tag(): doc is NULL");
+    return NULL;
+  }
+  if (! doc->pool) {
+    QX_LOGGER_FATAL("runtime exception: qs_new_tag(): doc->pool is NULL");
+    return NULL;
+  }
+
+  node = (Node *)apr_palloc(doc->pool, sizeof(Node));
   node->next      = NULL;
   node->parent    = NULL;
   node->child     = NULL;
@@ -141,8 +156,10 @@ qs_new_tag(Doc *doc)
 Node *
 qs_add_attr(Doc *doc, Node *node, Attr *attr) 
 {
-  if (node == NULL)
-    QX_LOGGER_FATAL("qs_add_attr() node is null");
+  if (node == NULL) {
+    QX_LOGGER_FATAL("runtime exception: qs_add_attr(): node is null");
+    return NULL;
+  }
 
   attr->parent = node;
   attr->next   = NULL;
diff --git a/test/qs_parse_tag/Makefile b/test/qs_parse_tag/Makefile
new file mode 100644 (file)
index 0000000..ee41e9a
--- /dev/null
@@ -0,0 +1,25 @@
+.SUFFIXES:
+.SUFFIXES: .c.o
+
+TARGET= test_qs_parse_tag
+
+TOP_DIR=../..
+
+INC_DIR=-I$(TOP_DIR)/include \
+  -I/usr/include/apache2 \
+  -I/usr/include/apr-1.0 \
+  -I$(TOP_DIR)/src
+
+all:test
+
+
+
+test: $(TARGET)
+       ./$(TARGET)
+
+$(TARGET): $(TARGET).c
+       gcc -g -o $@ -Wall -lcunit $< $(INC_DIR) -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE  -lapr-1 -laprutil-1 
+
+
+clean:
+       rm -f ./$(TARGET)
diff --git a/test/qs_parse_tag/test_qs_parse_tag.c b/test/qs_parse_tag/test_qs_parse_tag.c
new file mode 100644 (file)
index 0000000..9dce46c
--- /dev/null
@@ -0,0 +1,253 @@
+#include <CUnit/CUnit.h>
+#include <CUnit/Console.h>
+#include <CUnit/Basic.h>
+#include <stdio.h>
+#define CHXJ_TEST
+#define IMG_NOT_CONVERT_FILENAME
+
+#include "apr.h"
+#include "apr_strings.h"
+#include "chxj_apache.h"
+#include "chxj_str_util.h"
+#include "chxj_str_util.c"
+#include "qs_ignore_sp.c"
+#include "qs_log.c"
+#include "qs_malloc.c"
+#include "qs_parse_string.c"
+#include "qs_parse_attr.c"
+#include "qs_parse_tag.c"
+#include <iconv.h>
+
+
+/*===========================================================================*/
+/* qs_new_tag();                                                             */
+/*===========================================================================*/
+void test_qs_new_tag_001();
+void test_qs_new_tag_002();
+void test_qs_new_tag_003();
+/*===========================================================================*/
+/* qs_add_attr();                                                            */
+/*===========================================================================*/
+void test_qs_add_attr_001();
+void test_qs_add_attr_002();
+void test_qs_add_attr_003();
+/* pend */
+
+void test_log_rerror(const char *file, int line, int level, apr_status_t status, const request_rec *r, const char *fmt, ...)
+{
+  va_list ap;
+  fprintf(stderr, "ERROR LOG %s:%d ", file,line);
+  va_start(ap, fmt);
+  vfprintf(stderr, fmt,ap);
+  va_end(ap);
+  fprintf(stderr, "\n");
+}
+void test_log_error(const char *file, int line, int level, apr_status_t status, const request_rec *r, const char *fmt, ...)
+{
+  va_list ap;
+  fprintf(stderr, "ERROR LOG %s:%d ", file,line);
+  va_start(ap, fmt);
+  vfprintf(stderr, fmt,ap);
+  va_end(ap);
+  fprintf(stderr, "\n");
+}
+
+int
+main()
+{
+  CU_pSuite str_util_suite;
+  CU_initialize_registry();
+  str_util_suite = CU_add_suite("test qs_parse_tag.c", NULL, NULL);
+  /*=========================================================================*/
+  /* qs_new_tag()                                                            */
+  /*=========================================================================*/
+  CU_add_test(str_util_suite, "qs_new_tag() 001",                                  test_qs_new_tag_001);
+  CU_add_test(str_util_suite, "qs_new_tag() 002",                                  test_qs_new_tag_002);
+  CU_add_test(str_util_suite, "qs_new_tag() 003",                                  test_qs_new_tag_003);
+  /*=========================================================================*/
+  /* qs_add_attr()                                                           */
+  /*=========================================================================*/
+  CU_add_test(str_util_suite, "qs_add_attr() 001",                                 test_qs_add_attr_001);
+  CU_add_test(str_util_suite, "qs_add_attr() 002",                                 test_qs_add_attr_002);
+  CU_add_test(str_util_suite, "qs_add_attr() 003",                                 test_qs_add_attr_003);
+  /* aend */
+
+  CU_basic_run_tests();
+  CU_cleanup_registry();
+
+  return(0);
+}
+
+
+
+#define APR_INIT \
+  apr_pool_t *p; \
+  Doc doc;  \
+  do { \
+    apr_initialize(); \
+    apr_pool_create(&p, NULL); \
+    memset(&doc, 0, sizeof(Doc)); \
+    doc.pool = p; \
+  } \
+  while (0)
+
+#define APR_TERM \
+  do { \
+    apr_terminate(); \
+  } while (0)
+
+#define COOKIE_INIT(X) \
+  do { \
+    X.cookie_id = "test_cookie_id"; \
+  } while (0) \
+
+#define SPEC_INIT(X) \
+  do { \
+    X.html_spec_type = CHXJ_SPEC_Chtml_1_0; \
+  } while (0)
+
+
+
+
+/*===========================================================================*/
+/* qs_new_tag()                                                              */
+/*===========================================================================*/
+void test_qs_new_tag_001()
+{
+  Node *ret;
+  APR_INIT;
+
+  ret = qs_new_tag(&doc);
+  CU_ASSERT(ret != NULL);
+  CU_ASSERT(ret->next == NULL);
+  CU_ASSERT(ret->parent == NULL);
+  CU_ASSERT(ret->child == NULL);
+  CU_ASSERT(ret->child_tail == NULL);
+  CU_ASSERT(ret->attr == NULL);
+  CU_ASSERT(ret->attr_tail == NULL);
+  CU_ASSERT(ret->name == NULL);
+  CU_ASSERT(ret->value == NULL);
+
+  APR_TERM;
+}
+void test_qs_new_tag_002()
+{
+  Node *ret;
+  APR_INIT;
+
+  ret = qs_new_tag(NULL);
+  CU_ASSERT(ret == NULL);
+
+  APR_TERM;
+}
+void test_qs_new_tag_003()
+{
+  Node *ret;
+  APR_INIT;
+  doc.pool = NULL;
+  ret = qs_new_tag(&doc);
+  CU_ASSERT(ret == NULL);
+
+  APR_TERM;
+}
+/*===========================================================================*/
+/* qs_new_tag()                                                              */
+/*===========================================================================*/
+#if 0
+Node *
+qs_add_attr(Doc *doc, Node *node, Attr *attr)
+{
+  if (node == NULL) {
+    QX_LOGGER_FATAL("runtime exception: qs_add_attr(): node is null");
+    return NULL;
+  }
+
+  attr->parent = node;
+  attr->next   = NULL;
+
+  if (node->attr == NULL) {
+    node->attr      = attr;
+    node->attr_tail = attr;
+
+    return node;
+  }
+
+  node->attr_tail->next = attr;
+  node->attr_tail       = attr;
+
+  return node;
+}
+#endif
+void test_qs_add_attr_001()
+{
+  Node *node;
+  Node *ret;
+  Attr *attr;
+  APR_INIT;
+
+  node = qs_new_tag(&doc);
+  attr = qs_new_attr(&doc);
+  ret = qs_add_attr(&doc, node, attr);
+  CU_ASSERT(ret != NULL);
+  CU_ASSERT(ret == node);
+  CU_ASSERT(ret->attr      == attr);
+  CU_ASSERT(ret->attr_tail == attr);
+  CU_ASSERT(ret->attr->next == NULL);
+  CU_ASSERT(ret->attr->parent == node);
+
+  APR_TERM;
+}
+void test_qs_add_attr_002()
+{
+  Node *node;
+  Node *ret;
+  Attr *attr1;
+  Attr *attr2;
+  APR_INIT;
+
+  node = qs_new_tag(&doc);
+  attr1 = qs_new_attr(&doc);
+  attr2 = qs_new_attr(&doc);
+  ret = qs_add_attr(&doc, node, attr1);
+  ret = qs_add_attr(&doc, node, attr2);
+  CU_ASSERT(ret != NULL);
+  CU_ASSERT(ret == node);
+  CU_ASSERT(ret->attr == attr1);
+  CU_ASSERT(ret->attr->next == attr2);
+  CU_ASSERT(ret->attr_tail  == attr2);
+  CU_ASSERT(ret->attr->parent == node);
+  CU_ASSERT(ret->attr->next->parent == node);
+
+  APR_TERM;
+}
+void test_qs_add_attr_003()
+{
+  Node *node;
+  Node *ret;
+  Attr *attr1;
+  Attr *attr2;
+  Attr *attr3;
+  APR_INIT;
+
+  node = qs_new_tag(&doc);
+  attr1 = qs_new_attr(&doc);
+  attr2 = qs_new_attr(&doc);
+  attr3 = qs_new_attr(&doc);
+  ret = qs_add_attr(&doc, node, attr1);
+  ret = qs_add_attr(&doc, node, attr2);
+  ret = qs_add_attr(&doc, node, attr3);
+  CU_ASSERT(ret != NULL);
+  CU_ASSERT(ret == node);
+  CU_ASSERT(ret->attr == attr1);
+  CU_ASSERT(ret->attr->next == attr2);
+  CU_ASSERT(ret->attr->next->next == attr3);
+  CU_ASSERT(ret->attr_tail  == attr3);
+  CU_ASSERT(ret->attr->parent == node);
+  CU_ASSERT(ret->attr->next->parent == node);
+  CU_ASSERT(ret->attr->next->next->parent == node);
+
+  APR_TERM;
+}
+/*
+ * vim:ts=2 et
+ */