8bb2b1a9bb0ed60cfba92c2f15934b4d012090d9
[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 ImageTestData {
23         char *path; /* image filename */
24         char *expect_path; /* file path that we expect */
25         int type; /* image type */
26         int ret;  /* expected function return value */
27         int create_file; /* whether the file should be created */
28 } ImageTestData;
29
30 /* recursively deletes a directory only if it is under /tmp */
31 static void delete_only_tmp(char *path, int dir) {
32 #ifdef WIN32
33 #else
34         if (!strncmp(path, "/tmp/", 5) && BLI_exists(path)) {
35                 BLI_delete(path, dir, 1);
36         }
37 #endif
38 }
39
40 static void touch_only_tmp(char *path) {
41 #ifdef WIN32
42 #else
43         if (!strncmp(path, "/tmp/", 5)) {
44                 BLI_touch(path);
45         }
46 #endif
47 }
48
49 /* check that BKE_copy_images manipulates paths correctly */
50 START_TEST(test_copy_images)
51 {
52         char **dir;
53         ImageTestData *test;
54
55         /* XXX Windows not tested */    
56 #ifdef WIN32
57         static ImageTestData test_data[] = {
58                 {"//bar/image.png", "C:\\Temp\\bar\\image.png"},
59                 /* TODO add more */
60                 {NULL, NULL},
61         };
62         
63         BLI_strncpy(G.sce, "C:\\Temp\untitled.blend", sizeof(G.sce));
64 #else
65         /*
66           XXX are these paths possible in image->name?:
67
68           ./foo/image.png
69           ../foo/image.png
70
71           if so, BKE_copy_images currently doesn't support them!
72          */
73         static ImageTestData test_data[] = {
74                 {"//bar/image.png", "/tmp/blender/dest/bar/image.png", IMA_TYPE_IMAGE, 1, 1},
75                 {"//image.png", "/tmp/blender/dest/image.png", IMA_TYPE_IMAGE, 1, 1},
76                 {"//textures/test/foo/bar/image.png", "/tmp/blender/dest/textures/test/foo/bar/image.png", IMA_TYPE_IMAGE, 1, 1},
77                 {"//textures/test/foo/bar/image.png", "", IMA_TYPE_MULTILAYER, 0, 1},
78                 {"//./foo/bar/image.png", "/tmp/blender/dest/foo/bar/image.png", IMA_TYPE_IMAGE, 1, 1},
79                 {"//../foo/bar/image.png", "/tmp/blender/dest/image.png", IMA_TYPE_IMAGE, 1, 1},
80                 {"/tmp/blender/image.png", "/tmp/blender/dest/image.png", IMA_TYPE_IMAGE, 1, 1},
81                 /* expecting it to return 1 when src and dest are the same file */
82                 {"/tmp/blender/foo/bar/image.png", "/tmp/blender/dest/image.png", IMA_TYPE_IMAGE, 1, 1},
83                 {"/tmp/blender/dest/image.png", "/tmp/blender/dest/image.png", IMA_TYPE_IMAGE, 1, 1},
84                 /* expecting empty path and 0 return value for non-existing files */
85                 {"/tmp/blender/src/file-not-created", "", IMA_TYPE_IMAGE, 0, 0},
86                 {"", "", IMA_TYPE_IMAGE, 0, 0},
87                 {NULL, NULL},
88         };
89
90         char *dest_dir[] = {"/tmp/blender/dest/", "/tmp/blender/dest", NULL};
91         const char *blend_dir = "/tmp/blender/src";
92         
93         /* substitute G.sce */
94         BLI_snprintf(G.sce, sizeof(G.sce), "%s/untitled.blend", blend_dir);
95 #endif
96
97         /* only delete files/directories under /tmp/ ! */
98         delete_only_tmp(blend_dir, 1);
99
100         for (dir = dest_dir; *dir; dir++) {
101                 delete_only_tmp(*dir, 1);
102         }
103
104         /* create files */
105         BLI_recurdir_fileops(blend_dir);
106
107         /* create fake empty source files */
108         for (test= &test_data[0]; test->path; test++) {
109                 char dir[FILE_MAX];
110                 char path[FILE_MAX];
111
112                 if (!test->create_file) continue;
113
114                 /* expand "//" */
115                 BLI_strncpy(path, test->path, sizeof(path));
116                 BLI_convertstringcode(path, G.sce);
117
118                 /* create a directory */
119                 BLI_split_dirfile_basic(path, dir, NULL);
120                 BLI_recurdir_fileops(dir);
121
122                 /* create a file */
123                 touch_only_tmp(path);
124         }
125
126         for (dir = dest_dir; *dir; dir++) {
127                 for (test= &test_data[0]; test->path; test++) {
128                         Image image;
129                         char path[FILE_MAX];
130                         char part[200];
131                         int ret;
132
133                         BLI_strncpy(image.name, test->path, sizeof(image.name));
134                         image.type= test->type;
135
136                         ret= BKE_export_image(&image, *dir, path, sizeof(path));
137
138                         /* check if we got correct output */
139                         BLI_snprintf(part, sizeof(part), "For image with filename %s and type %d", test->path, test->type);
140                         fail_if(ret != test->ret, "%s, expected %d as return value got %d.", part, test->ret, ret);
141                         fail_if(strcmp(path, test->expect_path), "%s, expected path %s got \"%s\".", part, test->expect_path, path);
142                         if (test->ret == ret && ret == 1) {
143                                 fail_if(!BLI_exists(test->expect_path), "%s, expected %s to be created.", part, test->expect_path);
144                         }
145                 }
146         }
147 }
148 END_TEST
149
150 static Suite *image_suite(void)
151 {
152         Suite *s = suite_create("Image");
153
154         /* Core test case */
155         TCase *tc_core = tcase_create("Core");
156         tcase_add_test(tc_core, test_copy_images);
157         suite_add_tcase(s, tc_core);
158
159         return s;
160 }
161
162 int run_tests()
163 {
164         int totfail;
165         Suite *s = image_suite();
166         SRunner *sr = srunner_create(s);
167
168         /* run tests */
169         srunner_run_all(sr, CK_VERBOSE);
170
171         totfail= srunner_ntests_failed(sr);
172         srunner_free(sr);
173
174         return !totfail ? EXIT_SUCCESS : EXIT_FAILURE;
175 }