BRL-CAD
bu_progname.c
Go to the documentation of this file.
1 /* T E S T _ P R O G N A M E . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2011-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "bu.h"
28 
29 
30 int
31 main(int ac, char *av[])
32 {
33  int fail = 0;
34  const char *label;
35  const char *ans;
36  const char *res;
37  char *basename = (char *)bu_calloc(strlen(av[0]), sizeof(char), "bu_progname basename");
38 
39  if (ac > 1) {
40  fprintf(stderr,"Usage: %s\n", av[0]);
41  return 1;
42  }
43 
44  /* pre-define tests */
45  printf("Performing pre-defined tests:\n");
46  bu_basename(basename, av[0]);
47 
48  /* CASE 0: getting unset name */
49  label = "CASE 0";
50  res = bu_getprogname();
51  ans = "(BRL-CAD)";
52 
53  if (BU_STR_EQUAL(res, ans ? ans : "") || BU_STR_EQUAL(res, basename)) {
54  printf("%s: %24s -> %24s [PASSED]\n", label, "unset", res);
55  } else {
56  printf("%24s -> %24s (should be: %s) [FAIL]\n", label, res, ans);
57  fail++;
58  }
59 
60  /* CASE 1: try again unset */
61  label = "CASE 1";
62  res = bu_getprogname();
63  ans = "(BRL-CAD)";
64 
65  if (BU_STR_EQUAL(res, ans ? ans : "") || BU_STR_EQUAL(res, basename)) {
66  printf("%s: %24s -> %24s [PASSED]\n", label, "unset#2", res);
67  } else {
68  printf("%24s -> %24s (should be: %s) [FAIL]\n", label, res, ans);
69  fail++;
70  }
71 
72  /* CASE 2: set NULL, then get */
73  label = "CASE 2";
74  bu_setprogname(NULL);
75  res = bu_getprogname();
76  ans = "(BRL-CAD)";
77 
78  if (BU_STR_EQUAL(res, ans ? ans : "") || BU_STR_EQUAL(res, basename)) {
79  printf("%s: %24s -> %24s [PASSED]\n", label, "NULL", res);
80  } else {
81  printf("%24s -> %24s (should be: %s) [FAIL]\n", label, res, ans);
82  fail++;
83  }
84 
85  /* CASE 3: set, then get */
86  label = "CASE 3";
87  bu_setprogname(av[0]);
88  res = bu_getprogname();
89  ans = av[0];
90 
91  if (BU_STR_EQUAL(res, ans ? ans : "") || BU_STR_EQUAL(res, basename)) {
92  printf("%s: %24s -> %24s [PASSED]\n", label, "av[0]", res);
93  } else {
94  printf("%24s -> %24s (should be: %s) [FAIL]\n", label, res, ans);
95  fail++;
96  }
97 
98  bu_free(basename, "bu_progname basename");
99  /* CASE 4: set full, then get */
100  label = "CASE 4";
101  bu_setprogname(av[0]);
103  res = bu_getprogname();
104  ans = bu_argv0_full_path();
105  basename = (char *)bu_calloc(strlen(bu_argv0_full_path()), sizeof(char), "bu_progname basename");
106  bu_basename(basename, ans);
107 
108  if (BU_STR_EQUAL(res, ans ? ans : "") || BU_STR_EQUAL(res, basename)) {
109  printf("%s: %24s -> %24s [PASSED]\n", label, ans, res);
110  } else {
111  printf("%24s -> %24s (should be: %s) [FAIL]\n", label, res, ans);
112  fail++;
113  }
114 
115  /* CASE 5: set 2x, then get */
116  label = "CASE 5";
117  bu_setprogname(av[0]);
118  bu_setprogname("monkey_see_monkey_do");
119  res = bu_getprogname();
120  ans = "monkey_see_monkey_do";
121 
122  if (BU_STR_EQUAL(res, ans ? ans : "")) {
123  printf("%s: %24s -> %24s [PASSED]\n", label, ans, res);
124  } else {
125  printf("%24s -> %24s (should be: %s) [FAIL]\n", label, res, ans);
126  fail++;
127  }
128 
129  /* CASE 6: set 2x full path, then get */
130  label = "CASE 6";
132  bu_setprogname("/monkey/see/monkey/do");
133  res = bu_getprogname();
134  ans = "do";
135 
136  if (BU_STR_EQUAL(res, ans ? ans : "")) {
137  printf("%s: %24s -> %24s [PASSED]\n", label, "/monkey/see/monkey/do", res);
138  } else {
139  printf("%24s -> %24s (should be: %s) [FAIL]\n", label, res, ans);
140  fail++;
141  }
142 
143  /* CASE 7: get the full path */
144  label = "CASE 7";
145  bu_setprogname(av[0]);
146  res = bu_argv0_full_path();
147  bu_basename(basename, res);
148 
149  if (res[0] == BU_DIR_SEPARATOR) {
150  printf("%s: %24s -> %24s [PASSED]\n", label, basename, res);
151  } else {
152  printf("%24s -> %24s (should start with %c) [FAIL]\n", label, res, BU_DIR_SEPARATOR);
153  fail++;
154  }
155 
156  /* CASE 8: make sure bu_getprogname leaves a full path */
157  label = "CASE 8";
158  bu_setprogname("/monkey/see/monkey/do");
159  res = bu_getprogname();
160  res = bu_argv0_full_path();
161  if (BU_STR_EQUAL(res, "/monkey/see/monkey/do")) {
162  printf("%s: %24s -> %24s [PASSED]\n", label, res, res);
163  } else {
164  printf("%24s -> %24s (should match %s) [FAIL]\n", label, res, "/monkey/see/monkey/do");
165  fail++;
166  }
167 
168  bu_free(basename, "bu_progname basename");
169  return fail;
170 }
171 
172 
173 /*
174  * Local Variables:
175  * mode: C
176  * tab-width: 8
177  * indent-tabs-mode: t
178  * c-file-style: "stroustrup"
179  * End:
180  * ex: shiftwidth=4 tabstop=8
181  */
Header file for the BRL-CAD common definitions.
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
const char * bu_getprogname(void)
Definition: progname.c:96
int main(int ac, char *av[])
Definition: bu_progname.c:31
#define BU_DIR_SEPARATOR
Definition: defines.h:97
void bu_basename(char *basename, const char *path)
Definition: basename.c:30
ustring label
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
const char * bu_argv0_full_path(void)
Definition: progname.c:48
void bu_setprogname(const char *path)
Definition: progname.c:143
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126