Blame test/testutils.c

Packit cb6d3d
/*
Packit cb6d3d
  Copyright (C) 2014 Rocky Bernstein <rocky@gnu.org>
Packit cb6d3d
Packit cb6d3d
  This program is free software: you can redistribute it and/or modify
Packit cb6d3d
  it under the terms of the GNU General Public License as published by
Packit cb6d3d
  the Free Software Foundation, either version 3 of the License, or
Packit cb6d3d
  (at your option) any later version.
Packit cb6d3d
Packit cb6d3d
  This program is distributed in the hope that it will be useful,
Packit cb6d3d
  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit cb6d3d
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit cb6d3d
  GNU General Public License for more details.
Packit cb6d3d
Packit cb6d3d
  You should have received a copy of the GNU General Public License
Packit cb6d3d
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit cb6d3d
*/
Packit cb6d3d
Packit cb6d3d
/* test of routines in src/utils.h. For more verbse output, pass
Packit cb6d3d
   an argument. For example:
Packit cb6d3d
     make testutils
Packit cb6d3d
     ./testutils debug
Packit cb6d3d
Packit cb6d3d
*/
Packit cb6d3d
Packit cb6d3d
#ifdef HAVE_CONFIG_H
Packit cb6d3d
# include "config.h"
Packit cb6d3d
# define __CDIO_CONFIG_H__ 1
Packit cb6d3d
#endif
Packit cb6d3d
Packit cb6d3d
#include "../src/utils.h"
Packit cb6d3d
Packit cb6d3d
typedef struct {
Packit cb6d3d
    const char *fullname;
Packit cb6d3d
    const char *dirname;
Packit cb6d3d
    const char *basename;
Packit cb6d3d
} testdata;
Packit cb6d3d
Packit cb6d3d
int
Packit cb6d3d
main(int argc, const char *argv[])
Packit cb6d3d
{
Packit cb6d3d
  int i_rc=0;
Packit cb6d3d
  int i;
Packit cb6d3d
  const testdata paths[] = {
Packit cb6d3d
    {"/abc/def", "/abc/", "def"},
Packit cb6d3d
    {"hij/klm", "hij/",   "klm"},
Packit cb6d3d
    {"1234567890/klm", NULL,   NULL}
Packit cb6d3d
  };
Packit cb6d3d
  char path[10];
Packit cb6d3d
  for (i=0; i<3; i++) {
Packit cb6d3d
    char *fullpath = strdup(paths[i].fullname);
Packit cb6d3d
    char *base = split_base_dir(fullpath, path, sizeof(path));
Packit cb6d3d
    if (base == NULL) {
Packit cb6d3d
      if (paths[i].basename != NULL) {
Packit cb6d3d
	fprintf(stderr, "Got an unexpected null. Expected %s\n",
Packit cb6d3d
		paths[i].basename);
Packit cb6d3d
      } else {
Packit cb6d3d
	if (argc > 1) printf("%s is too large as expected\n", fullpath);
Packit cb6d3d
      }
Packit cb6d3d
    } else {
Packit cb6d3d
      if (argc > 1) {
Packit cb6d3d
	printf("dirname of %s is %s; basename is %s\n",
Packit cb6d3d
	       fullpath, path, base);
Packit cb6d3d
      }
Packit cb6d3d
      if (0 != strcmp(paths[i].basename, base)) {
Packit cb6d3d
	fprintf(stderr,
Packit cb6d3d
		"Expecting basename %s; got %s for %s\n",
Packit cb6d3d
		paths[i].basename, base, fullpath);
Packit cb6d3d
	i_rc = 1;
Packit cb6d3d
      }
Packit cb6d3d
      if (0 != strcmp(paths[i].dirname, path)) {
Packit cb6d3d
	fprintf(stderr,
Packit cb6d3d
		"Expecting dirname %s; got %s for %s\n",
Packit cb6d3d
		paths[i].dirname, path, fullpath);
Packit cb6d3d
	i_rc = 1;
Packit cb6d3d
      }
Packit cb6d3d
    }
Packit cb6d3d
    if (fullpath) free(fullpath);
Packit cb6d3d
  }
Packit cb6d3d
  exit(i_rc);
Packit cb6d3d
}