dimanche 29 mars 2015

How to test if value was assigned to struct?

I have the following unit test:



void testFileMapNoDirPath() {
int argc = 3;
char* argv[3];

int idx = 0;
argv[idx] = "rzsharp.exe";
argv[++idx] = "f";
argv[++idx] = "test.rz";

FileMap fmap = getFileMap(argc, argv);
int hasDir = hasDirectory(&fmap);

if (hasDir) {
printf("%%TEST_FAILED%% time=0 testname=testFileMapNoDirPath (testfilemap) message=fmap.dirPath has a value\n");
}
}


...which tests the following function:



int hasDirectory(FileMap *fmap){
return fmap->dirPath != '\0';
}


...getFileMap parses argument switches as they would be passed in from main:



FileMap getFileMap(int argc, char** argv) {
int i;
char* fileName = "\0";
char* dirPath = "\0";

for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-d") == 0) {
dirPath = argv[++i];
printf("found directory: %s\n", dirPath);
}
else if (strcmp(argv[i], "f") == 0) {
fileName = argv[++i];
printf("found file: %s\n", fileName);
}
}
FileMap fMap = {.fileName = fileName, .dirPath = dirPath};
return fMap;
}


...and FileMap:



typedef struct filemap {
const char* dirPath[128];
const char* fileName[32];

} FileMap;


The test is supposed to be able to tell me if a directory path was provided. How can I test if dirPath has a value?


Aucun commentaire:

Enregistrer un commentaire