📄 tst_parm.c
字号:
/* Tests of parameter utility functions * * Copyright (c) 1995, Advanced RISC Machines Limited. * All Rights Reserved. * * $Revision: 1.4.6.1 $ * $Author: rivimey $ * $Date: 1997/12/10 18:50:27 $ */#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#include "params.h"#include "logging.h"void LogError(char *format,...){ va_list args; va_start(args, format); printf("DERR> "); vprintf(format, args); va_end(args); exit(3);}void LogWarning(char *format,...){ va_list args; va_start(args, format); printf("DWAR> "); vprintf(format, args); va_end(args);}static void output_buffer(unsigned char *buffer, unsigned int n){ unsigned int i; for (i = 0; i < n; ++i) printf("%02x ", buffer[i]); putchar('\n');}static void output_config(const ParameterConfig * c){ unsigned int i; printf("\tnum_parameters = %d\n", c->num_parameters); for (i = 0; i < c->num_parameters; ++i) printf("\t\t%d:\t(%x,\t%d [%x])\n", i, c->param[i].type, c->param[i].value, c->param[i].value);}static void output_options(const ParameterOptions * o){ unsigned int i; printf("\tnum_param_lists = %d\n", o->num_param_lists); for (i = 0; i < o->num_param_lists; ++i) { unsigned int j; const ParameterList *list = &o->param_list[i]; printf("\t\t%d: %x * %d = (", i, list->type, list->num_options); for (j = 0; j < list->num_options; ++j) { printf("%d [%x]", list->option[j], list->option[j]); if (j < (list->num_options - 1)) printf(", "); } printf(")\n"); }}int main(int argc, char *argv[]){ Parameter cp1[] = { {AP_BAUD_RATE, 19200}, {AP_CAFE_MENU, 0xBEEF}}; ParameterConfig c1 = {sizeof(cp1) / sizeof(Parameter), cp1}; Parameter cp2[3] = { {0, 0}, {0, 0}, {0, 0}}; ParameterConfig c2 = {3, cp2}; const ParameterConfig *c3 = NULL; unsigned int ol1[] = {230400, 115200, 57600, 38400, 19200, 9600, 1200}; unsigned int ol2[] = {0xBEEF, 0xF00D}; ParameterList l1[] = { {AP_BAUD_RATE, sizeof(ol1) / sizeof(unsigned int), ol1}, {AP_CAFE_MENU, sizeof(ol2) / sizeof(unsigned int), ol2} }; ParameterOptions o1 = {sizeof(l1) / sizeof(ParameterList), l1}; unsigned int ol3[3][5] = { {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0} }; ParameterList l2[3] = { {0, 5, ol3[0]}, {0, 5, ol3[1]}, {0, 5, ol3[2]} }; ParameterOptions o2 = {3, l2}; unsigned int v1, v2; unsigned char buffer[256]; unsigned int n; bool fail = FALSE; n = Angel_BuildParamConfigMessage(buffer, &c1); printf("Wrote %d chars:\n", n); output_buffer(buffer, n); if (Angel_ReadParamConfigMessage(buffer, &c2)) { printf("Read succeeded\n"); output_config(&c2); } else { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("Read failed\n"); } n = Angel_BuildParamOptionsMessage(buffer, &o1); printf("Wrote %d chars:\n", n); output_buffer(buffer, n); if (Angel_ReadParamOptionsMessage(buffer, &o2)) { printf("Read succeeded\n"); output_options(&o2); } else { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("Read failed\n"); } printf("AP_PARAMS_START = %x, AP_BAUD_RATE = %x, AP_PARAMS_END = %x\n", AP_PARAMS_START, AP_BAUD_RATE, AP_PARAMS_END); printf("AP_NUM_PARAMS = %d\n", AP_NUM_PARAMS); c3 = Angel_MatchParams(&o2, &o1); if (c3 == NULL) { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("Match failed\n"); } else { printf("Match succeeded\n"); output_config(c3); if ((!Angel_FindParam(AP_BAUD_RATE, c3, &v1)) || (v1 != 38400) || (!Angel_FindParam(AP_CAFE_MENU, c3, &v2)) || (v2 != 0xBEEF) ) { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("Bad config selected\n"); } } o1.param_list[1].num_options = 1; o1.param_list[1].option[0] = 0xf00d; c3 = Angel_MatchParams(&o2, &o1); if (c3 == NULL) { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("Match failed\n"); } else { printf("Match succeeded\n"); output_config(c3); if ((!Angel_FindParam(AP_BAUD_RATE, c3, &v1)) || (v1 != 38400) || (!Angel_FindParam(AP_CAFE_MENU, c3, &v2)) || (v2 != 0xF00D) ) { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("Bad config selected\n"); } } o1.param_list[1].option[0] = 0xfeed; c3 = Angel_MatchParams(&o2, &o1); if (c3 == NULL) printf("Match failed\n"); else { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("Match succeeded\n"); output_config(c3); } o1.param_list[1].type = 0xdead; c3 = Angel_MatchParams(&o2, &o1); if (c3 == NULL) printf("Match failed\n"); else { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("Match succeeded\n"); output_config(c3); } if (Angel_FindParam(AP_BAUD_RATE, &c1, &v1)) { printf("Found baud rate %d in c1\n", v1); if (v1 != 19200) { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("Expected 19200\n"); } } else { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("No baud rate in c1\n"); } if (Angel_FindParam(0xfeed, &c1, &v1)) { fail = TRUE; printf("TEST FAILED at line %d\n", __LINE__); printf("Found feed %d in c1\n", v1); } else printf("No feed in c1\n"); if (fail) { printf("TESTS FAILED :-(\n"); return 1; } else { printf("Tests passed :-)\n"); return 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -