summaryrefslogtreecommitdiff
path: root/tests/piece-table-internals.c
blob: 6cf5014b9e770142e65b7597617efcccd896c0a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../src/piece-table-internals.h"

size_t
count_pieces (piece_table *pt)
{
  size_t count;
  piece *p;
  for (count = 0, p = pt->sentinel->next; p != pt->sentinel; p = p->next)
    count++;
  return count;
}

int
main ()
{
  char tmp[100];
  size_t expected_count;
  piece_table *pt = piece_table_create_from_str ("0123456789", 10);
  /** Inserting **/
  /* Should add pieces... */
  expected_count = 1;
  assert (count_pieces (pt) == expected_count);

  piece_table_insert (pt, 10, "abcdefgh", 8);
  expected_count++;
  assert (count_pieces (pt) == expected_count);

  /* ... but not always **/
  piece_table_insert (pt, 18, "abcdefgh", 8);
  assert (count_pieces (pt) == expected_count);


  /** Deleting **/
  piece_table_delete (pt, 23, 3);
  assert (count_pieces (pt) == expected_count);

  piece_table_delete (pt, 13, 4);
  expected_count++;
  assert (count_pieces (pt) == expected_count);

  piece_table_delete (pt, 0, 10);
  expected_count--;
  assert (count_pieces (pt) == expected_count);


  /** Result **/
  piece_table_to_string (pt, tmp, 99);
  assert (0 == strcmp (tmp, "abchabcde"));

  /** Optimize **/
  piece_table_optimize (pt);
  expected_count = 1;
  assert (count_pieces (pt) == expected_count);

  /** Result **/
  piece_table_to_string (pt, tmp, 99);
  assert (0 == strcmp (tmp, "abchabcde"));

  piece_table_destroy (pt);

  return 0;
}