cmdlineoptions/c/tests.c

186 lines
7.1 KiB
C
Raw Permalink Normal View History

2014-02-22 21:25:41 +01:00
/*
* tests.c
*
* Created on: 22.02.2014
* Author: qwc
*/
#include "cmdlineoptions.h"
2014-03-09 17:16:13 +01:00
#include <stdio.h>
#include <stdlib.h>
2014-02-22 21:25:41 +01:00
2014-03-09 17:16:13 +01:00
void configureDefaultSet() {
2014-02-22 21:25:41 +01:00
CmdLO_Init(1);
CmdLO_Add("test", "--test");
CmdLO_Add("test", "-t");
CmdLO_Add("example", "--example");
CmdLO_Add("example", "-e");
CmdLO_Add("something", "-s");
CmdLO_AddDefaultParameter("something", "sometest");
2014-03-09 17:16:13 +01:00
CmdLO_AddDescription("test", "Well, this is a test command line option.");
2014-02-22 21:25:41 +01:00
CmdLO_AddDescription("example", "An example commandline option");
2014-03-09 17:16:13 +01:00
CmdLO_AddPossibleParameter("example", "test");
CmdLO_AddPossibleParameter("example", "test2");
2014-02-22 21:25:41 +01:00
}
2014-03-09 17:16:13 +01:00
#define ASSERT(x,y) if(!(x)) printf("ASSERT FAILED! '%s'.\n",y); else printf("ASSERT '%s' succeded.\n",y)
2014-02-22 21:25:41 +01:00
int main(int argc, char** argv) {
2014-02-22 21:47:27 +01:00
// configure the options
configureDefaultSet();
2014-02-22 21:25:41 +01:00
2014-02-22 21:47:27 +01:00
// emulate parameters
int myargc = 0;
char** myargv = 0;
// test 1 parse nothing.
2014-03-09 17:16:13 +01:00
printf("Test 1: no parameters.\n");
CmdLO_Parse(myargc, myargv);
2014-02-22 21:47:27 +01:00
// check contents...
2014-03-09 17:16:13 +01:00
// nothing should be set so...
ASSERT(CmdLO_NodeGet("test")->option->set == 0, "test not set.");
ASSERT(CmdLO_NodeGet("example")->option->set == 0, "example not set.");
ASSERT(CmdLO_NodeGet("something")->option->set == 0, "something not set");
ASSERT(CmdLO_NodeGet("test")->option->optionscount == 2,
"test option count");
ASSERT(CmdLO_NodeGet("example")->option->optionscount == 2,
"example option count");
ASSERT(CmdLO_NodeGet("something")->option->optionscount == 1,
"something option count");
ASSERT(CmdLO_NodeGet("test")->option->defaultparametercount == 0,
"test defaultparameter");
ASSERT(CmdLO_NodeGet("example")->option->defaultparametercount == 0,
"example defaultparameter");
ASSERT(CmdLO_NodeGet("something")->option->defaultparametercount == 1,
"something defaultparameter");
ASSERT(CmdLO_NodeGet("test")->option->possibleparametercount == 0,
"test possibleparameters");
ASSERT(CmdLO_NodeGet("example")->option->possibleparametercount == 2,
"example possibleparameters");
ASSERT(CmdLO_NodeGet("something")->option->possibleparametercount == 0,
"something possibleparameters");
ASSERT(CmdLO_NodeGet("test")->option->valuecount == 0, "test value count");
ASSERT(CmdLO_NodeGet("example")->option->valuecount == 0,
"example value count");
ASSERT(CmdLO_NodeGet("something")->option->valuecount == 0,
"something value count");
printf("Test 2: all parameters set with allowed parameters.\n");
2014-02-22 21:47:27 +01:00
// test 2 parse everything
2014-03-09 17:16:13 +01:00
//configureDefaultSet();
myargc = 6;
myargv = calloc(myargc, sizeof(char*));
myargv[1] = "--test";
myargv[2] = "tested";
myargv[3] = "-e";
myargv[4] = "test";
myargv[5] = "-s";
CmdLO_Parse(myargc, myargv);
ASSERT(CmdLO_NodeGet("test")->option->set != 0, "test set.");
ASSERT(CmdLO_NodeGet("example")->option->set != 0, "example set.");
ASSERT(CmdLO_NodeGet("something")->option->set != 0, "something set");
ASSERT(CmdLO_NodeGet("test")->option->optionscount == 2,
"test option count");
ASSERT(CmdLO_NodeGet("example")->option->optionscount == 2,
"example option count");
ASSERT(CmdLO_NodeGet("something")->option->optionscount == 1,
"something option count");
ASSERT(CmdLO_NodeGet("test")->option->defaultparametercount == 0,
"test defaultparameter");
ASSERT(CmdLO_NodeGet("example")->option->defaultparametercount == 0,
"example defaultparameter");
ASSERT(CmdLO_NodeGet("something")->option->defaultparametercount == 1,
"something defaultparameter");
ASSERT(CmdLO_NodeGet("test")->option->possibleparametercount == 0,
"test possibleparameters");
ASSERT(CmdLO_NodeGet("example")->option->possibleparametercount == 2,
"example possibleparameters");
ASSERT(CmdLO_NodeGet("something")->option->possibleparametercount == 0,
"something possibleparameters");
ASSERT(CmdLO_NodeGet("test")->option->valuecount == 1, "test value count");
ASSERT(CmdLO_NodeGet("example")->option->valuecount == 1,
"example value count");
ASSERT(CmdLO_NodeGet("something")->option->valuecount == 0,
"something value count");
2014-02-22 21:47:27 +01:00
// test 3 parse something
2014-03-09 17:16:13 +01:00
CmdLO_Destroy();
configureDefaultSet();
printf("Test 3: Just some options.\n");
myargc = 4;
myargv[1] = "--test";
myargv[2] = "blah";
myargv[3] = "testtttt";
//myargv[4] = "-h";
CmdLO_Parse(myargc, myargv);
ASSERT(CmdLO_NodeGet("test")->option->set != 0, "test set.");
ASSERT(CmdLO_NodeGet("example")->option->set == 0, "example set.");
ASSERT(CmdLO_NodeGet("something")->option->set == 0, "something set");
ASSERT(CmdLO_NodeGet("test")->option->optionscount == 2,
"test option count");
ASSERT(CmdLO_NodeGet("example")->option->optionscount == 2,
"example option count");
ASSERT(CmdLO_NodeGet("something")->option->optionscount == 1,
"something option count");
ASSERT(CmdLO_NodeGet("test")->option->defaultparametercount == 0,
"test defaultparameter");
ASSERT(CmdLO_NodeGet("example")->option->defaultparametercount == 0,
"example defaultparameter");
ASSERT(CmdLO_NodeGet("something")->option->defaultparametercount == 1,
"something defaultparameter");
ASSERT(CmdLO_NodeGet("test")->option->possibleparametercount == 0,
"test possibleparameters");
ASSERT(CmdLO_NodeGet("example")->option->possibleparametercount == 2,
"example possibleparameters");
ASSERT(CmdLO_NodeGet("something")->option->possibleparametercount == 0,
"something possibleparameters");
ASSERT(CmdLO_NodeGet("test")->option->valuecount == 2, "test value count");
ASSERT(CmdLO_NodeGet("example")->option->valuecount == 0,
"example value count");
ASSERT(CmdLO_NodeGet("something")->option->valuecount == 0,
"something value count");
2014-02-22 21:47:27 +01:00
// test 4 producing some errors...
2014-03-09 17:16:13 +01:00
CmdLO_Destroy();
configureDefaultSet();
printf("Test 4: now lets produce some errors...\n");
myargc = 5;
myargv[1] = "-tehe";
myargv[2] = "wtf";
myargv[3] = "-e";
myargv[4] = "12345";
CmdLO_Parse(myargc, myargv);
ASSERT(CmdLO_NodeGet("test")->option->set == 0, "test set.");
ASSERT(CmdLO_NodeGet("example")->option->set == 0, "example set.");
ASSERT(CmdLO_NodeGet("something")->option->set == 0, "something set");
ASSERT(CmdLO_NodeGet("test")->option->optionscount == 2,
"test option count");
ASSERT(CmdLO_NodeGet("example")->option->optionscount == 2,
"example option count");
ASSERT(CmdLO_NodeGet("something")->option->optionscount == 1,
"something option count");
ASSERT(CmdLO_NodeGet("test")->option->defaultparametercount == 0,
"test defaultparameter");
ASSERT(CmdLO_NodeGet("example")->option->defaultparametercount == 0,
"example defaultparameter");
ASSERT(CmdLO_NodeGet("something")->option->defaultparametercount == 1,
"something defaultparameter");
ASSERT(CmdLO_NodeGet("test")->option->possibleparametercount == 0,
"test possibleparameters");
ASSERT(CmdLO_NodeGet("example")->option->possibleparametercount == 2,
"example possibleparameters");
ASSERT(CmdLO_NodeGet("something")->option->possibleparametercount == 0,
"something possibleparameters");
ASSERT(CmdLO_NodeGet("test")->option->valuecount == 0, "test value count");
ASSERT(CmdLO_NodeGet("example")->option->valuecount == 0,
"example value count");
ASSERT(CmdLO_NodeGet("something")->option->valuecount == 0,
"something value count");
2014-02-22 21:25:41 +01:00
return 0;
}