/ tests / src / imgs / set.cpp
set.cpp
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   set.cpp                                            :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: lfiorell <lfiorell@student.42.fr>          +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2025/02/14 10:56:33 by lfiorell          #+#    #+#             */
  9  /*   Updated: 2025/02/14 12:21:46 by lfiorell         ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "CUnit/Basic.h"
 14  
 15  extern "C"
 16  {
 17  #include "img/set.h"
 18  #include "mlx.h"
 19  }
 20  
 21  static char *path = (char *)"data/set.xpm";
 22  static t_2d tile_size = {24, 24};
 23  static void *mlx = mlx_init();
 24  
 25  void test_set_from_xpm(void)
 26  {
 27  
 28    t_set *set = crust_set_from_xpm(mlx, path, tile_size);
 29  
 30    CU_ASSERT_PTR_NOT_NULL_FATAL(set);
 31    CU_ASSERT_PTR_NOT_NULL(set->mlx_ptr);
 32    CU_ASSERT_PTR_NOT_NULL(set->img);
 33    CU_ASSERT_EQUAL(set->tile_size.x, 24);
 34    CU_ASSERT_EQUAL(set->tile_size.y, 24);
 35  
 36    crust_set_drop(set);
 37  }
 38  
 39  void test_set_get_img(void)
 40  {
 41    t_set *set = crust_set_from_xpm(mlx, path, tile_size);
 42    t_img *img = crust_set_get_img(set, 0);
 43  
 44    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
 45    CU_ASSERT_PTR_NOT_NULL(img->mlx_ptr);
 46    CU_ASSERT_PTR_NOT_NULL(img->img_ptr);
 47    CU_ASSERT_EQUAL(img->width, 24);
 48    CU_ASSERT_EQUAL(img->height, 24);
 49  
 50    for (int y = 0; y < img->height; ++y)
 51    {
 52      for (int x = 0; x < img->width; ++x)
 53      {
 54        t_2d pos = {x, y};
 55        t_rgba pixel = crust_img_get_pixel(img, pos);
 56        t_rgba expected = crust_img_get_pixel(set->img, pos);
 57  
 58        CU_ASSERT_EQUAL(pixel.r, expected.r);
 59        CU_ASSERT_EQUAL(pixel.g, expected.g);
 60        CU_ASSERT_EQUAL(pixel.b, expected.b);
 61      }
 62    }
 63  
 64    crust_set_drop(set);
 65    crust_img_drop(img);
 66  }
 67  
 68  void test_set_get_img_off(void)
 69  {
 70    t_set *set = crust_set_from_xpm(mlx, path, tile_size);
 71    t_img *img = crust_set_get_img(set, 1);
 72  
 73    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
 74    CU_ASSERT_PTR_NOT_NULL(img->mlx_ptr);
 75    CU_ASSERT_PTR_NOT_NULL(img->img_ptr);
 76    CU_ASSERT_EQUAL(img->width, 24);
 77    CU_ASSERT_EQUAL(img->height, 24);
 78  
 79    for (int y = 0; y < img->height; ++y)
 80    {
 81      for (int x = 0; x < img->width; ++x)
 82      {
 83        t_2d pos = {x, y};
 84        t_rgba pixel = crust_img_get_pixel(img, pos);
 85        t_2d src_pos;
 86        src_pos.x = x + 24;
 87        src_pos.y = y;
 88        t_rgba expected = crust_img_get_pixel(set->img, src_pos);
 89  
 90        CU_ASSERT_EQUAL(pixel.r, expected.r);
 91        CU_ASSERT_EQUAL(pixel.g, expected.g);
 92        CU_ASSERT_EQUAL(pixel.b, expected.b);
 93      }
 94    }
 95  
 96    crust_set_drop(set);
 97    crust_img_drop(img);
 98  }
 99  
100  void test_set_get_imgs(void)
101  {
102    int start = 0;
103    int end = 2;
104  
105    t_set *set = crust_set_from_xpm(mlx, path, tile_size);
106    CU_ASSERT_PTR_NOT_NULL_FATAL(set);
107    t_img *img = crust_set_get_imgs(set, start, end);
108    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
109  
110    for (int y = 0; y < img->height; ++y)
111    {
112      for (int x = 0; x < img->width; ++x)
113      {
114        t_2d pos = {x, y};
115        t_rgba pixel = crust_img_get_pixel(img, pos);
116        t_rgba expected = crust_img_get_pixel(set->img, pos);
117  
118        CU_ASSERT_EQUAL(pixel.r, expected.r);
119        CU_ASSERT_EQUAL(pixel.g, expected.g);
120        CU_ASSERT_EQUAL(pixel.b, expected.b);
121      }
122    }
123  
124    crust_set_drop(set);
125    crust_img_drop(img);
126  }
127  
128  void test_set_get_imgs_off(void)
129  {
130    int start = 3;
131    int end = 5;
132  
133    t_set *set = crust_set_from_xpm(mlx, path, tile_size);
134    CU_ASSERT_PTR_NOT_NULL_FATAL(set);
135    t_img *img = crust_set_get_imgs(set, start, end);
136    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
137  
138    t_2d base_pos;
139    base_pos.x = 24 * 3;
140    base_pos.y = 0;
141  
142    for (int y = 0; y < img->height; ++y)
143    {
144      for (int x = 0; x < img->width; ++x)
145      {
146        t_2d pos = {x, y};
147        t_rgba pixel = crust_img_get_pixel(img, pos);
148        t_2d src_pos;
149        src_pos.x = x + base_pos.x;
150        src_pos.y = y + base_pos.y;
151        t_rgba expected = crust_img_get_pixel(set->img, src_pos);
152  
153        CU_ASSERT_EQUAL(pixel.r, expected.r);
154        CU_ASSERT_EQUAL(pixel.g, expected.g);
155        CU_ASSERT_EQUAL(pixel.b, expected.b);
156      }
157    }
158  
159    crust_set_drop(set);
160    crust_img_drop(img);
161  }
162  
163  void test_set_get_imgs_offy(void)
164  {
165    int start = 12;
166    int end = 14;
167  
168    t_set *set = crust_set_from_xpm(mlx, path, tile_size);
169    CU_ASSERT_PTR_NOT_NULL_FATAL(set);
170    t_img *img = crust_set_get_imgs(set, start, end);
171    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
172  
173    t_2d base_pos;
174    base_pos.x = 0;
175    base_pos.y = 24;
176  
177    for (int y = 0; y < img->height; ++y)
178    {
179      for (int x = 0; x < img->width; ++x)
180      {
181        t_2d pos = {x, y};
182        t_rgba pixel = crust_img_get_pixel(img, pos);
183        t_2d src_pos;
184        src_pos.x = x + base_pos.x;
185        src_pos.y = y + base_pos.y;
186        t_rgba expected = crust_img_get_pixel(set->img, src_pos);
187  
188        CU_ASSERT_EQUAL(pixel.r, expected.r);
189        CU_ASSERT_EQUAL(pixel.g, expected.g);
190        CU_ASSERT_EQUAL(pixel.b, expected.b);
191      }
192    }
193  
194    crust_set_drop(set);
195    crust_img_drop(img);
196  }
197  
198  void test_get_imgs_by_pos(void)
199  {
200    t_2d start;
201    start.x = 0;
202    start.y = 0;
203    t_2d end;
204    end.x = 2;
205    end.y = 2;
206  
207    t_set *set = crust_set_from_xpm(mlx, path, tile_size);
208    CU_ASSERT_PTR_NOT_NULL_FATAL(set);
209    t_img *img = crust_set_get_imgs_by_pos(set, start, end);
210    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
211  
212    for (int y = 0; y < img->height; ++y)
213    {
214      for (int x = 0; x < img->width; ++x)
215      {
216        t_2d pos = {x, y};
217        t_rgba pixel = crust_img_get_pixel(img, pos);
218        t_rgba expected = crust_img_get_pixel(set->img, pos);
219  
220        CU_ASSERT_EQUAL(pixel.r, expected.r);
221        CU_ASSERT_EQUAL(pixel.g, expected.g);
222        CU_ASSERT_EQUAL(pixel.b, expected.b);
223      }
224    }
225  
226    crust_set_drop(set);
227    crust_img_drop(img);
228  }
229  
230  void test_get_imgs_by_pos_off(void)
231  {
232    t_2d start;
233    start.x = 3;
234    start.y = 3;
235    t_2d end;
236    end.x = 5;
237    end.y = 5;
238  
239    t_set *set = crust_set_from_xpm(mlx, path, tile_size);
240    CU_ASSERT_PTR_NOT_NULL_FATAL(set);
241    t_img *img = crust_set_get_imgs_by_pos(set, start, end);
242    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
243  
244    t_2d base_pos;
245    base_pos.x = 24 * 3;
246    base_pos.y = 24 * 3;
247  
248    for (int y = 0; y < img->height; ++y)
249    {
250      for (int x = 0; x < img->width; ++x)
251      {
252        t_2d pos = {x, y};
253        t_rgba pixel = crust_img_get_pixel(img, pos);
254        t_2d src_pos;
255        src_pos.x = x + base_pos.x;
256        src_pos.y = y + base_pos.y;
257        t_rgba expected = crust_img_get_pixel(set->img, src_pos);
258  
259        CU_ASSERT_EQUAL(pixel.r, expected.r);
260        CU_ASSERT_EQUAL(pixel.g, expected.g);
261        CU_ASSERT_EQUAL(pixel.b, expected.b);
262      }
263    }
264  
265    crust_set_drop(set);
266    crust_img_drop(img);
267  }
268  
269  void run_set_tests(void)
270  {
271    CU_pSuite suite = CU_add_suite("Set", NULL, NULL);
272  
273    CU_add_test(suite, "crust_set_from_xpm", test_set_from_xpm);
274    CU_add_test(suite, "crust_set_get_img", test_set_get_img);
275    CU_add_test(suite, "crust_set_get_img_off", test_set_get_img_off);
276    CU_add_test(suite, "crust_set_get_imgs", test_set_get_imgs);
277    CU_add_test(suite, "crust_set_get_imgs_off", test_set_get_imgs_off);
278    CU_add_test(suite, "crust_set_get_imgs_offy", test_set_get_imgs_offy);
279    CU_add_test(suite, "crust_set_get_imgs_by_pos", test_get_imgs_by_pos);
280    CU_add_test(suite, "crust_set_get_imgs_by_pos_off", test_get_imgs_by_pos_off);
281  }