CUnit testing void functions - c

Hi Im new to Cunit tests and testing in general. I cant find a way to test this void functon. Wondering how to properly do it or how to refactor it.
Any tips on how to properly test void functions? Thanks.
void validate_arg(int argc, char *argv[], struct Statistics *stats) {
if (argc == 4 && strcmp(argv[2], "-G") == 0) {
FILE *file = fopen(argv[3], "w");
stop_prog(write_stats(file, stats), err1);
fclose(file);
}
free(stats);
}
The Cunit test I wrote but I cant assert anything since its a void and I cant test if my file to write was created since since it gets deallocated inside the if.
void test_validate_args_0(void) {
struct Stats *stats = malloc(sizeof(struct Stats));
char* argv[] = {"TEST", "dummy", "-G", NULL};
CU_ASSERT_EQUAL(validate_arg(4, argv, stats), 0);
free(stats);
}

Related

Using the TCL interpreter repeatedly inside a C program

I wish to use a c program repeatedly run the TCL interpreter many times. For reasons that are complicated, I need this to be a pure C program and not something that is embedded as a shared object. As an example, I wish to run this simple tcl program, tryMe.tcl, twice:
prtstr "Test from tryMe.tcl"
The prtstr is a TCL function that I have written that for right now just writes to stdout. Below is the c code that is attempting to interpret the tryMe.tcl program twice.
I compile the program below like this under linux:
$ gcc -c try.c; gcc -o try try.o -ltcl;
and run it like this:
$ ./try tryMe.tcl
And I get zero output. What am I doing wrong? Also are there steps required to reset the tcl interpreter so that it will be fresh each time.
#define _GNU_SOURCE
#include <tcl/tcl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int PrintStrObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
char *str;
int len;
Tcl_Obj *objPtr;
int i;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "value");
return TCL_ERROR;
}
objPtr = objv[1];
str = Tcl_GetStringFromObj(objPtr, &len);
if (str[0] == '\0')
return TCL_ERROR;
printf("len: %d, str: %s\n", len, str);
return TCL_OK;
}
int Tcl_AppInit(Tcl_Interp* interp)
{
if (Tcl_Init(interp) == TCL_ERROR)
return TCL_ERROR;
Tcl_CreateObjCommand(interp,"prtstr", PrintStrObjCmd, (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
return TCL_OK;
}
int main(int argc, char *argv[])
{
char *cmd = NULL;
Tcl_Interp * interp = Tcl_CreateInterp();
Tcl_AppInit(interp);
asprintf(&cmd, "%s -x -y -z", argv[1]);
Tcl_Eval(interp, cmd);
free(cmd);
asprintf(&cmd, "%s -q -r -s 2", argv[1]);
Tcl_Eval(interp, cmd);
exit(0);
}
Thanks very much!
You should check out the Tcler's Wiki on this, as the pattern of embedding a Tcl interpreter in your application is a known and supported one. It includes a worked example that you can adapt (and no, I didn't write it; I prefer to extend standard Tcl interpreters).
The main problem you've got is that you're not calling Tcl_FindExecutable(). In modern Tcl, that initialises a number of key subsystems in the library (including its high-performance memory allocator!) so it's slightly vital. In your case, you've got a real argv available so you can use argv[0] with it:
Tcl_FindExecutable(argv[0]);
// NULL would also work as an argument, in a pinch at least
Once you've done that, you can call other Tcl API functions, specifically Tcl_CreateInterp().
You have a minor problem in that you are not testing the results of calls for failure. In C, this is essential as you don't have exceptions to do the heavy lifting of error handling for you.
Thanks for the pointer to TCLer's Wiki. That helped. I didn't understand that the script in Tcl_Eval(interp,script) was not a file name but a character string containing a tcl program. So this program uses TCL_Evalfile() I also wanted to be able to pass command line arguments to the tcl program. I found out how but diving into the TCL source for Tcl_MainEx(). Below is a program that does what I want. Also I discovered that calling Tcl_EvalFile more than one does retains the state so if I want a fresh value of the interpreter, I'll have to delete the old one and create a new one everytime.
#include <tcl/tcl.h>
#include <stdio.h>
#include <stdlib.h>
int PrintStrObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
char *str;
int len;
Tcl_Obj *objPtr;
int i;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "value");
return TCL_ERROR;
}
objPtr = objv[1];
str = Tcl_GetStringFromObj(objPtr, &len);
if (str[0] == '\0')
return TCL_ERROR;
printf("len: %d, str: %s\n", len, str);
return TCL_OK;
}
int Tcl_AppInit(Tcl_Interp* interp)
{
if (Tcl_Init(interp) == TCL_ERROR)
return TCL_ERROR;
Tcl_CreateObjCommand(interp,"prtstr", PrintStrObjCmd, (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
return TCL_OK;
}
int main(int argc, char **argv)
{
char *script = argv[1];
Tcl_Obj *argvPtr;
Tcl_FindExecutable(script);
Tcl_Interp *interp = Tcl_CreateInterp();
if (interp == NULL) {
fprintf(stderr,"Cannot create TCL interpreter\n");
exit(-1);
}
if (Tcl_AppInit(interp) != TCL_OK)
return TCL_ERROR;
Tcl_SetVar2Ex(interp, "argv0", NULL, Tcl_NewStringObj(script,-1), TCL_GLOBAL_ONLY);
argc -= 2;
argv += 2;
Tcl_SetVar2Ex(interp, "argc", NULL, Tcl_NewIntObj(argc), TCL_GLOBAL_ONLY);
argvPtr = Tcl_NewListObj(0, NULL);
while (argc--)
Tcl_ListObjAppendElement(NULL, argvPtr, Tcl_NewStringObj(*argv++, -1));
Tcl_SetVar2Ex(interp, "argv", NULL, argvPtr, TCL_GLOBAL_ONLY);
if (Tcl_EvalFile(interp, script) != TCL_OK)
return TCL_ERROR;
exit(0);
}

How to invoke the help function with optparse-c

I am trying to use the optparse-c library. This library seems to me quite useful. What I find not
so clear is how to invoke the help without calling prog -h.
I can do it if I remove the keyword static from the function signature static void printopts(FILE *f) in
optparse.c, then I could do:
int main(int argc, char **argv)
{
int i;
opt_basename(argv[0], '/');
if (opt_parse("usage: %s [options] arg1 arg2", options, argv) != 2) {
printopts(stderr);
exit(EXIT_FAILURE);
}
...
However, it seems to me the author of this library had some other idea how to invoke the help function. I would be happy if someone could help me figure this out.
I also tried this:
char h[] = "-h";
opt_basename(argv[0], '/');
if (opt_parse("usage: %s [options] arg1 arg2", options, argv) != 2) {
argc++;
strcpy(argv[1], h);
opt_parse("usage: %s [options] arg1 arg2", options, argv);
exit(EXIT_FAILURE);
}
Oh well, after a lot of poking and looking into the source code, I found out the not trivial (as I am still learning C) solution:
if (opt_parse("usage: %s [options] arg1 arg2", options, argv) != 2) {
opt_help(0, (void *)0);
exit(EXIT_FAILURE);
}

C - using global structure

I'm using global structure so I can set/get error or state from everywhere. It was working fine, now I'm in trouble.
structure
typedef struct program Program;
struct program
{
int error;
int state;
};
// global declarations
Program *program;
init
void init_memory(void)
{
program = malloc(sizeof(Program));
if(program == NULL)
{
print_error(E_MEM_ALLOC);
exit(EXIT_FAILURE);
}
program->state = S_NONE;
program->error = E_OK;
}
here program crashes, when I remove "program->error = ...." program is working fine...i don't know why :/
void check_file(char *filename)
{
FILE *file = fopen(filename, "r");
if(file == NULL)
{
program->error = E_FILE_OPEN;
return;
}
fclose(file);
}
whole program: http://pastebin.com/dwSVQ9x8
Making program a pointer seems unnecessary, why not just make it the actual struct:
Program program = { .error = E_OK, .state = S_NONE };
Then you don't need to worry about allocating it (and can indeed remove init_memory altogether). Just change access to its members to use . instead of ->, i.e., program.error = E_FILE_OPEN.

What is the purpose of memset in C

I am reading REBOL source code and I can't understand the purpose of the following statement:
/***********************************************************************
**
*/ int main(int argc, char **argv)
/*
***********************************************************************/
{
char *cmd;
// Parse command line arguments. Done early. May affect REBOL boot.
Parse_Args(argc, argv, &Main_Args);
Print_Str("REBOL 3.0\n");
REBOL_Init(&Main_Args);
// Evaluate user input:
while (TRUE) {
cmd = Prompt_User();
REBOL_Do_String(cmd);
if (!IS_UNSET(DS_TOP)) {
//if (DSP > 0) {
if (!IS_ERROR(DS_TOP)) {
Prin("== ");
Print_Value(DS_TOP, 0, TRUE);
} else
Print_Value(DS_TOP, 0, FALSE);
//}
}
//DS_DROP; // result
}
return 0;
}
In Parse_Args function:
/***********************************************************************
**
*/ void Parse_Args(int argc, REBCHR **argv, REBARGS *rargs)
/*
** Parse REBOL's command line arguments, setting options
** and values in the provided args structure.
**
***********************************************************************/
{
REBCHR *arg;
REBCHR *args = 0; // holds trailing args
int flag;
int i;
CLEARS(rargs);
....
And CLEARS is defined:
#define CLEARS(m) memset((void*)(m), 0, sizeof(*m));
So my question is why memset is being used here?
It looks like rargs is some kind of struct containing options for the program. CLEARS() and memset() is used to fill that struct with zero values to initiate it.
memset sets a block of memory to a given value.
It is normally written in hand optimized assembly and is expected to be blindingly fast.

Unexpected Results using fts_children() in C

I have been beating my head on a wall over this fts_children() question. In the man page, http://www.kernel.org/doc/man-pages/online/pages/man3/fts.3.html, it clearly states As a special case, if fts_read() has not yet been called for a hierarchy,
fts_children() will return a pointer to the files in the logical directory
specified to fts_open(), that is, the arguments specified to fts_open().
Which I take to mean that a linked list of all the files in the current directory are returned. Well, I am finding that not to be the case and I would really appreciate some help in the matter. I expected a linked list to be returned and then I would iterate through it to find the file with the matching file name (the end goal). However, right now, I am just trying to iterate through the linked list (baby steps). Right now, it will return one file and then exit the loop. This does not make sense to me. Any help would very much appreciated!!!
Opening of file system:
char* const path[PATH_MAX] = {directory_name(argv[argc-index]), NULL};
char* name = file_name(argv[argc-index]);
if ((file_system = fts_open(path, FTS_COMFOLLOW, NULL)) == NULL){
fprintf(stderr,"%s:%s\n", strerror(errno), getprogname());
exit(EXIT_FAILURE);
}/*Ends the files system check if statement*/
/*Displays the information about the specified file.*/
file_ls(file_system,name, flags);
For clarification, the directory_name parses the inputted path from the user and returns something like /home/tpar44. That directory is then opened.
Searching within the file system:
void
file_ls(FTS* file_system, char* file_name, int* flags){
FTSENT* parent = NULL;
//dint stop = 0;
parent = fts_children(file_system, 0);
while( parent != NULL ){
printf("parent = %s\n", parent->fts_name);
parent = parent->fts_link;
}
}
Thanks!
I think this is entirely by design.
...that is, the arguments specified to fts_open()...
What it says is that it will list the root elements in the path_argv parameters for your convenenience. It treats the path_argv array as a logical directory itself.
In other words this:
int main(int argc, char* const argv[])
{
char* const path[] = { ".", "/home", "more/root/paths", NULL };
FTS* file_system = fts_open(path, FTS_COMFOLLOW | FTS_NOCHDIR, &compare);
if (file_system)
{
file_ls(file_system, "", 0);
fts_close(file_system);
}
return 0;
}
Will output
parent = .
parent = /home
parent = more/root/paths
Which, in fact, it does (see http://liveworkspace.org/code/c2d794117eae2d8af1166ccd620d29eb).
Here is a more complete sample that shows complete directory traversal:
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fts.h>
#include<string.h>
#include<errno.h>
int compare (const FTSENT**, const FTSENT**);
void file_ls(FTS* file_system, const char* file_name, int* flags)
{
FTSENT* node = fts_children(file_system, 0);
if (errno != 0)
perror("fts_children");
while (node != NULL)
{
// TODO use file_name and flags
printf("found: %s%s\n", node->fts_path, node->fts_name);
node = node->fts_link;
}
}
int main(int argc, char* const argv[])
{
FTS* file_system = NULL;
FTSENT* node = NULL;
if (argc<2)
{
printf("Usage: %s <path-spec>\n", argv[0]);
exit(255);
}
char* const path[] = { argv[1], NULL };
const char* name = "some_name";
file_system = fts_open(path, FTS_COMFOLLOW | FTS_NOCHDIR, &compare);
if (file_system)
{
file_ls(file_system, name, 0); // shows roots
while( (node = fts_read(file_system)) != NULL)
file_ls(file_system, name, 0); // shows child elements
fts_close(file_system);
}
return 0;
}
int compare(const FTSENT** one, const FTSENT** two)
{
return (strcmp((*one)->fts_name, (*two)->fts_name));
}

Resources