测试与debug。

This commit is contained in:
Saturneric 2018-06-13 16:32:39 +08:00
parent 896e454c40
commit 880fa14d10
3 changed files with 133 additions and 115 deletions

16
test.c
View File

@ -37,6 +37,12 @@ int list(void) {
return 0; return 0;
} }
int _useTreeThroughDown(TNode *p_tnode, unsigned long long height) {
printTNode(p_tnode,0);
return 0;
}
int tree(void) { int tree(void) {
safeModeForTree(1); safeModeForTree(1);
Tree *t_tree = initTree(); Tree *t_tree = initTree();
@ -44,12 +50,14 @@ int tree(void) {
addChildInRight(t_tnode, cl_tnode); addChildInRight(t_tnode, cl_tnode);
addChildInRight(t_tnode, cr_tnode); addChildInRight(t_tnode, cr_tnode);
addChildInRight(cl_tnode, tnodeWithInt(4)); addChildInRight(cl_tnode, tnodeWithInt(4));
addChildInRight(cl_tnode, tnodeWithInt(5)); TNode *gs_tnode = tnodeWithInt(5);
addChildInRight(cl_tnode,gs_tnode);
addChildInRight(cr_tnode, tnodeWithInt(6)); addChildInRight(cr_tnode, tnodeWithInt(6));
addChildInRight(cr_tnode, tnodeWithInt(7)); addChildInRight(cr_tnode, tnodeWithInt(7));
addChildInRight(gs_tnode, tnodeWithInt(8));
removeChildByIndex(cr_tnode, 1); setRoot(t_tree, t_tnode);
printTNodeWithFamily(t_tnode, 0); TreeThroughUp(t_tree, _useTreeThroughDown);
//printTNodeWithFamily(t_tnode, 0);
releaseAllForTree(); releaseAllForTree();
return 0; return 0;
} }

View File

@ -107,7 +107,7 @@ int addChildInRight(TNode *f_tnode, TNode *c_tnode) {
TNode *getBrotherInLeft(TNode *p_tnode) { TNode *getBrotherInLeft(TNode *p_tnode) {
List *p_home = p_tnode->father->home; List *p_home = p_tnode->father->home;
Node *p_node = p_home->head; Node *p_node = p_tnode->room;
unsigned long long index = getIndexByNode(p_home, p_node); unsigned long long index = getIndexByNode(p_home, p_node);
if (index > 0) return (TNode *)(findByIndexForNode(p_home, index - 1)->value); if (index > 0) return (TNode *)(findByIndexForNode(p_home, index - 1)->value);
return NULL; return NULL;
@ -293,7 +293,6 @@ int removeChildByIndex(TNode *p_tnode, unsigned long long index) {
int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) {
TNode *p_tnode = p_tree->root; TNode *p_tnode = p_tree->root;
if (p_tnode != NULL) { if (p_tnode != NULL) {
func(p_tnode, 0);
if (p_tnode->child_num > 0) { if (p_tnode->child_num > 0) {
for (int i = 0; i < p_tnode->child_num; i++) { for (int i = 0; i < p_tnode->child_num; i++) {
if (_doTreeThroughUp(getChildByIndex(p_tnode, i), 1, func) == -1) { if (_doTreeThroughUp(getChildByIndex(p_tnode, i), 1, func) == -1) {
@ -301,6 +300,7 @@ int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height))
} }
} }
} }
func(p_tnode, 0);
} }
return 0; return 0;
} }
@ -320,8 +320,8 @@ int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned lo
int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) {
TNode *p_tnode = p_tree->root; TNode *p_tnode = p_tree->root;
if (p_tree->root != NULL) { if (p_tree->root != NULL) {
func(p_tnode, 0);
if (p_tree->root->child_num > 0) { if (p_tree->root->child_num > 0) {
func(p_tnode, 0);
for (int i = 0; i < p_tnode->child_num; i++) { for (int i = 0; i < p_tnode->child_num; i++) {
if (_doTreeThroughDown(getChildByIndex(p_tnode, i), 1, func) == -1) { if (_doTreeThroughDown(getChildByIndex(p_tnode, i), 1, func) == -1) {
break; break;

View File

@ -58,135 +58,145 @@ void *getValueByPointerForTree(TNode *p_tnode) {
} }
int printTNode(TNode *p_tnode, int priority) { int printTNode(TNode *p_tnode, int priority) {
for (int i = 0; i < priority; i++) printf(" "); if (p_tnode != NULL) {
if (priority == 0) printf("###"); for (int i = 0; i < priority; i++) printf(" ");
else printf("#"); if (priority == 0) printf("###");
printf("TNode(id: %llu)\n", p_tnode->id); else printf("#");
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("ifMalloc: "); printf("TNode(id: %llu)\n", p_tnode->id);
if (p_tnode->if_malloc) {
printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" "); for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type); printf("ifMalloc: ");
if (!strcmp(p_tnode->type, "int")) { if (p_tnode->if_malloc) {
printf("%d\n", *(int *)(p_tnode->value)); printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) {
printf("%d\n", *(int *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "double")) {
printf("%a\n", *(double *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "string")) {
printf("%s\n", (char *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "pointer")) {
printf("%p\n", (char *)(p_tnode->value));
}
} }
else if (!strcmp(p_tnode->type, "double")) { else printf("NO\n");
printf("%a\n", *(double *)(p_tnode->value));
} if (p_tnode->child_num > 0) {
else if (!strcmp(p_tnode->type, "string")) { for (int i = 0; i < priority + 1; i++) printf(" ");
printf("%s\n", (char *)(p_tnode->value)); printf("Child number: %llu\n", p_tnode->child_num);
}
else if (!strcmp(p_tnode->type, "pointer")) {
printf("%p\n", (char *)(p_tnode->value));
} }
return 0;
} }
else printf("NO\n"); return -1;
if (p_tnode->child_num > 0) {
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Child number: %llu\n", p_tnode->child_num);
}
return 0;
} }
int printTNodeWithHome(TNode *p_tnode,int priority) { int printTNodeWithHome(TNode *p_tnode,int priority) {
if (priority == 0) printf("###"); if (p_tnode != NULL) {
else printf("#"); if (priority == 0) printf("###");
printf("TNode(id: %llu)\n", p_tnode->id); else printf("#");
for (int i = 0; i < priority + 1; i++) printf(" "); printf("TNode(id: %llu)\n", p_tnode->id);
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" "); for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type); printf("ifMalloc: ");
if (!strcmp(p_tnode->type, "int")) { if (p_tnode->if_malloc) {
printf("%d\n", *(int *)(p_tnode->value)); printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) {
printf("%d\n", *(int *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "double")) {
printf("%a\n", *(double *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "string")) {
printf("%s\n", (char *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "pointer")) {
printf("%p\n", (char *)(p_tnode->value));
}
} }
else if (!strcmp(p_tnode->type, "double")) { else printf("NO\n");
printf("%a\n", *(double *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "string")) {
printf("%s\n", (char *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "pointer")) {
printf("%p\n", (char *)(p_tnode->value));
}
}
else printf("NO\n");
if (p_tnode->father != NULL) { if (p_tnode->father != NULL) {
for (int i = 0; i < priority + 1; i++) printf(" "); for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Father id: %llu\n", p_tnode->father->id); printf("Father id: %llu\n", p_tnode->father->id);
} }
else else
{ {
for (int i = 0; i < priority + 1; i++) printf(" "); for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Father: NO\n"); printf("Father: NO\n");
} }
if (p_tnode->child_num > 0) {
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Child(number: %llu):\n", p_tnode->child_num);
}
List *p_home = p_tnode->home; if (p_tnode->child_num > 0) {
Node *p_node = p_home->head; for (int i = 0; i < priority + 1; i++) printf(" ");
while (p_node != NULL) { printf("Child(number: %llu):\n", p_tnode->child_num);
printTNode((TNode *)p_node->value,priority+2); }
p_node = p_node->next;
List *p_home = p_tnode->home;
Node *p_node = p_home->head;
while (p_node != NULL) {
printTNode((TNode *)p_node->value, priority + 2);
p_node = p_node->next;
}
return 0;
} }
return 0; return -1;
} }
int printTNodeWithFamily(TNode *p_tnode, int priority) { int printTNodeWithFamily(TNode *p_tnode, int priority) {
for (int i = 0; i < priority ; i++) printf(" "); if (p_tnode != NULL) {
if (priority == 0) printf("###"); for (int i = 0; i < priority; i++) printf(" ");
else printf("#"); if (priority == 0) printf("###");
printf("TNode(id: %llu)\n", p_tnode->id); else printf("#");
for (int i = 0; i < priority + 1; i++) printf(" "); printf("TNode(id: %llu)\n", p_tnode->id);
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" "); for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type); printf("ifMalloc: ");
if (!strcmp(p_tnode->type, "int")) { if (p_tnode->if_malloc) {
printf("%d\n", *(int *)(p_tnode->value)); printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) {
printf("%d\n", *(int *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "double")) {
printf("%a\n", *(double *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "string")) {
printf("%s\n", (char *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "pointer")) {
printf("%p\n", (char *)(p_tnode->value));
}
} }
else if (!strcmp(p_tnode->type, "double")) { else printf("NO\n");
printf("%a\n", *(double *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "string")) {
printf("%s\n", (char *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "pointer")) {
printf("%p\n", (char *)(p_tnode->value));
}
}
else printf("NO\n");
if (p_tnode->father != NULL) { if (p_tnode->father != NULL) {
for (int i = 0; i < priority + 1; i++) printf(" "); for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Father id: %llu\n", p_tnode->father->id); printf("Father id: %llu\n", p_tnode->father->id);
} }
else else
{ {
for (int i = 0; i < priority + 1; i++) printf(" "); for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Father: NO\n"); printf("Father: NO\n");
} }
if (p_tnode->child_num > 0) { if (p_tnode->child_num > 0) {
for (int i = 0; i < priority + 1; i++) printf(" "); for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Child(number: %llu):\n", p_tnode->child_num); printf("Child(number: %llu):\n", p_tnode->child_num);
} }
List *p_home = p_tnode->home; List *p_home = p_tnode->home;
Node *p_node = p_home->head; Node *p_node = p_home->head;
while (p_node != NULL) { while (p_node != NULL) {
printTNodeWithFamily((TNode *)p_node->value, priority + 2); printTNodeWithFamily((TNode *)p_node->value, priority + 2);
p_node = p_node->next; p_node = p_node->next;
}
return 0;
} }
return 0; return -1;
} }
int printTree(Tree *p_tree) { int printTree(Tree *p_tree) {