Merge with 2.5 -r 22173:22620.
[blender.git] / source / creator / tests / alltest.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <check.h>
4
5 #include "MEM_guardedalloc.h"
6
7 #include "BKE_blender.h"
8 #include "BKE_image.h"
9 #include "BKE_utildefines.h"
10 #include "BKE_global.h"
11
12 #include "BLI_listbase.h"
13 #include "BLI_util.h"
14 #include "BLI_fileops.h"
15 #include "BLI_string.h"
16
17 #include "DNA_image_types.h"
18
19 char bprogname[FILE_MAXDIR+FILE_MAXFILE];
20 char btempdir[FILE_MAXDIR+FILE_MAXFILE];
21
22 typedef struct ImageTestResult {
23         char *path;
24         char *rel;
25         int ret;
26 } ImageTestResult;
27
28 typedef struct ImageTestData {
29         char *path; /* image filename */
30         ImageTestResult result[10];
31 } ImageTestData;
32
33 /* check that BKE_copy_images manipulates paths correctly */
34 START_TEST(test_copy_images)
35 {
36         char **dir;
37         ImageTestData *test;
38         int i,j;
39
40 #ifdef WIN32
41         /* TBD... */
42 #else
43         /*
44           XXX are these paths possible in image->name?:
45
46           ./foo/image.png
47           ../foo/image.png
48
49           if so, BKE_copy_images currently doesn't support them!
50          */
51
52         const char *blend_dir = "/home/user/foo";
53         char *dest_dir[] = {"/home/user/", "/home/user", "/home/user/export/", "/home/user/foo/", NULL};
54
55         static ImageTestData test_data[] = {
56
57                 /* image path | [expected output path | corresponding relative path | expected return value] */
58
59                 /* relative, 0 level deep */
60                 {"//image.png", {{"/home/user/image.png", "image.png", 1},
61                                                  {"/home/user/image.png", "image.png", 1},
62                                                  {"/home/user/export/image.png", "image.png", 1},
63                                                  {"/home/user/foo/image.png", "image.png", 2},}},
64
65                 /* relative, 1 level deep */
66                 {"//bar/image.png", {{"/home/user/bar/image.png", "bar/image.png", 1},
67                                                          {"/home/user/bar/image.png", "bar/image.png", 1},
68                                                          {"/home/user/export/bar/image.png", "bar/image.png", 1},
69                                                          {"/home/user/foo/bar/image.png", "bar/image.png", 2},}},
70
71                 /* relative, 2 level deep */
72                 {"//bar/foo/image.png", {{"/home/user/bar/foo/image.png", "bar/foo/image.png", 1},
73                                                                  {"/home/user/bar/foo/image.png", "bar/foo/image.png", 1},
74                                                                  {"/home/user/export/bar/foo/image.png", "bar/foo/image.png", 1},
75                                                                  {"/home/user/foo/bar/foo/image.png", "bar/foo/image.png", 2},}},
76
77                 /* absolute, not under .blend dir */
78                 {"/home/user/bar/image.png", {{"/home/user/image.png", "image.png", 1},
79                                                                           {"/home/user/image.png", "image.png", 1},
80                                                                           {"/home/user/export/image.png", "image.png", 1},
81                                                                           {"/home/user/foo/image.png", "image.png", 1},}},
82
83                 /* absolute, under .blend dir, 0 level deep */
84                 {"/home/user/foo/image.png", {{"/home/user/image.png", "image.png", 1},
85                                                                           {"/home/user/image.png", "image.png", 1},
86                                                                           {"/home/user/export/image.png", "image.png", 1},
87                                                                           {"/home/user/foo/image.png", "image.png", 2},}},
88
89                 /* absolute, under .blend dir, 1 level deep */
90                 {"/home/user/foo/bar/image.png", {{"/home/user/bar/image.png", "bar/image.png", 1},
91                                                                                   {"/home/user/bar/image.png", "bar/image.png", 1},
92                                                                                   {"/home/user/export/bar/image.png", "bar/image.png", 1},
93                                                                                   {"/home/user/foo/bar/image.png", "bar/image.png", 2},}},
94
95                 /* absolute, under .blend dir, 2 level deep */
96                 {"/home/user/foo/bar/foo/image.png", {{"/home/user/bar/foo/image.png", "bar/foo/image.png", 1},
97                                                                                           {"/home/user/bar/foo/image.png", "bar/foo/image.png", 1},
98                                                                                           {"/home/user/export/bar/foo/image.png", "bar/foo/image.png", 1},
99                                                                                           {"/home/user/foo/bar/foo/image.png", "bar/foo/image.png", 2},}},
100
101                 /* empty image path, don't let these pass! */
102                 {"", {{"", 0},
103                           {"", 0},
104                           {"", 0}}},
105
106                 {NULL},
107         };
108         
109         /* substitute G.sce */
110         BLI_snprintf(G.sce, sizeof(G.sce), "%s/untitled.blend", blend_dir);
111 #endif
112
113         for (dir= dest_dir, i= 0; *dir; dir++, i++) {
114                 for (test= &test_data[0]; test->path; test++) {
115                         Image image;
116                         char path[FILE_MAX];
117                         char rel[FILE_MAX];
118                         char part[200];
119                         int ret;
120
121                         BLI_strncpy(image.name, test->path, sizeof(image.name));
122
123                         /* passing NULL as abs path or rel path or both shouldn't break it */
124                         int abs_rel_null[][2]= {{0, 0}, {1, 0}, {0, 1}, {1, 1}, {-1}};
125
126                         for (j= 0; abs_rel_null[j][0] != -1; j++) {
127
128                                 int *is_null= abs_rel_null[j];
129
130                                 ret= BKE_get_image_export_path(&image, *dir,
131                                                                                            is_null[0] ? NULL : path, sizeof(path),
132                                                                                            is_null[1] ? NULL : rel, sizeof(rel));
133
134                                 BLI_snprintf(part, sizeof(part), "For image at %s (output abs path is %s, rel path is %s)",
135                                                          test->path, is_null[0] ? "NULL" : "non-NULL", is_null[1] ? "NULL" : "non-NULL");
136
137                                 /* we should get what we expect */
138                                 ImageTestResult *res= &test->result[i];
139                                 fail_if(ret != res->ret, "%s, expected to return %d got %d.", part, res->ret, ret);
140
141                                 if (!is_null[0] && res->path)
142                                         fail_if(strcmp(path, res->path), "%s, expected absolute path \"%s\" got \"%s\".", part, res->path, path);
143                                 if (!is_null[1] && res->rel)
144                                         fail_if(strcmp(rel, res->rel), "%s, expected relative path \"%s\" got \"%s\".", part, res->rel, rel);
145                         }
146                 }
147         }
148 }
149 END_TEST
150
151 static Suite *image_suite(void)
152 {
153         Suite *s= suite_create("Image");
154
155         /* Core test case */
156         TCase *tc_core= tcase_create("Core");
157         tcase_add_test(tc_core, test_copy_images);
158         suite_add_tcase(s, tc_core);
159
160         return s;
161 }
162
163 int run_tests()
164 {
165         int totfail;
166         Suite *s= image_suite();
167         SRunner *sr= srunner_create(s);
168
169         /* run tests */
170         srunner_run_all(sr, CK_VERBOSE);
171
172         totfail= srunner_ntests_failed(sr);
173         srunner_free(sr);
174
175         return !totfail ? EXIT_SUCCESS : EXIT_FAILURE;
176 }