/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to tests/dynamic_array.c

  • Committer: Gustav Hartvigsson
  • Date: 2015-04-10 21:36:10 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20150410213610-r1q96ym4nmulpax9
* Added a few skeletal functions to Callback.h
* Re-wrote parts of SDynamicArray to be more sane.
  * removed s_dynamic_array_len() and s_dynamic_array_add()
  * added s_dynamic_array_set() and s_dynamic_array_last_item()
  * use calloc instead of malloc.
* added unsubscribe function to SGlobalNotify.
* Added decumentation in Interface.h
* Made s_dbg_print() more helpful.

-- Testing --
* Made test_case() macro more useful.
* added tests for the dynamic array.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "SimpleTypeSystem.h"
 
2
#include "test_macros.h"
 
3
#include <stdlib.h>
 
4
#include <time.h>
 
5
 
 
6
int test_dynamic_array (void) {
 
7
  setup_unit ();
 
8
  
 
9
  SDynamicArray * array = NULL;
 
10
  array = s_dynamic_array_new (20, NULL);
 
11
  
 
12
  test_case (array != NULL, "Array is not null");
 
13
  
 
14
  test_case (s_dynamic_array_size (array) == 20, "The array maximum size is 20");
 
15
  
 
16
  s_print ("Adding a random number to a random possision in the array"
 
17
           " (bettween 0 and 20).\n");
 
18
  
 
19
  srand(time(NULL));
 
20
  
 
21
  int r_n = rand ();
 
22
  int r_p = rand () % 20;
 
23
  
 
24
  s_dynamic_array_set (array, r_p, (spointer) r_n);
 
25
  
 
26
  test_case ((int) s_dynamic_array_get (array, r_p) == r_n,
 
27
             "The the inserted random number matches the generated number.");
 
28
  
 
29
  s_print ("Adding a number (12345) to index 21\n");
 
30
  
 
31
  s_dynamic_array_set (array, 21, (spointer) 12345);
 
32
  
 
33
  test_case (s_dynamic_array_size (array) > 20, "The new size is larger then the old size.");
 
34
  
 
35
  s_print ("Filling array with numbers: 0-32\n");
 
36
  
 
37
  for (int i = 0; i < s_dynamic_array_size (array); i++) {
 
38
    s_dynamic_array_set (array, i, (spointer) i);
 
39
  }
 
40
  for (int i = 0; i < s_dynamic_array_size (array); i++) {
 
41
    int got_val = s_dynamic_array_get(array, i);
 
42
    test_case (got_val == i, "The stored value (%d) matches the expected value (%d).", i, got_val);
 
43
  }
 
44
  
 
45
  end_unit ();
 
46
}