summaryrefslogtreecommitdiff
path: root/tests/maptest.c
blob: 25f7f3219f035378fd33868d7ae9c767b56acab4 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
  Copyright (C) 2003 GraphicsMagick Group
 
  This program is covered by multiple licenses, which are described in
  Copyright.txt. You should have received a copy of Copyright.txt with this
  package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.

 Test MagickMap key,value map functionality

 Written by Bob Friesenhahn <bfriesen@simple.dallas.tx.us>
 
*/

#include <magick/api.h>
#include <magick/map.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _KeyValMap
{
  char *key;
  char *value;
} KeyValMap;


int main ( int argc, char **argv )
{
  int
    exit_status = 0,
    i;

  ExceptionInfo
    exception;

  MagickMap
    map;

  MagickMapIterator
    iterator;

  const char
    *key;

  static const KeyValMap
    KeyVal[] =
    {
      { "0", "number 0" },
      { "1", "number 1" },
      { "2", "number 2" },
      { "3", "number 3" },
      { "4", "number 4" },
      { "5", "number 5" },
      { "6", "number 6" },
      { "7", "number 7" },
      { "8", "number 8" },
      { "9", "number 9" },
      { 0, 0 }
    };

  (void) argc;
  (void) argv;

  GetExceptionInfo(&exception);
  map=MagickMapAllocateMap(MagickMapCopyString,MagickMapDeallocateString);

  (void) printf("Adding map entries ...\n");
  for (i=0; KeyVal[i].key; i++)
    {
      (void) MagickMapAddEntry(map,KeyVal[i].key,(void *)KeyVal[i].value,0,&exception);
    }


  {
    char
      str_index[MaxTextExtent];

    (void) printf("Keyed access ...\n");
    for (i=0; KeyVal[i].key; i++)
      {
        FormatString(str_index,"%u",i);
        (void) printf("key=\"%s\" value=\"%s\"\n", str_index,
                      (char *)MagickMapAccessEntry(map,str_index,0));
      }
  }

  iterator=MagickMapAllocateIterator(map);
  (void) printf("Iterate forward ...\n");
  while(MagickMapIterateNext(iterator,&key))
    (void) printf("key=%s value=%s\n",key,
                  (char *) MagickMapDereferenceIterator(iterator,0));

  (void) printf("Iterate reverse ...\n");
  while(MagickMapIteratePrevious(iterator,&key))
    (void) printf("key=%s value=%s\n",key,
                  (char *) MagickMapDereferenceIterator(iterator,0));

  i=2;
  (void) printf("Remove entry for key \"%s\" and then iterate forward ...\n",
                KeyVal[i].key);
  (void) MagickMapRemoveEntry(map,KeyVal[i].key);
  while(MagickMapIterateNext(iterator,&key))
    (void) printf("key=%s value=%s\n",key,
                  (char *)MagickMapDereferenceIterator(iterator,0));

  (void) printf("Iterate reverse ...\n");
  while(MagickMapIteratePrevious(iterator,&key))
    (void) printf("key=%s value=%s\n",key,
                  (char *)MagickMapDereferenceIterator(iterator,0));

  MagickMapDeallocateIterator(iterator);

  i=2;
  (void) MagickMapAddEntry(map,KeyVal[i].key,(void *)KeyVal[i].value,0,&exception);
  (void) printf("Add entry for key \"%s\" and then iterate forward ...\n",
                KeyVal[i].key);

  iterator=MagickMapAllocateIterator(map);  
  while(MagickMapIterateNext(iterator,&key))
    (void) printf("key=%s value=%s\n",key,
                  (char *)MagickMapDereferenceIterator(iterator,0));

  MagickMapDeallocateIterator(iterator);
  MagickMapDeallocateMap(map);

  DestroyExceptionInfo(&exception);
  return exit_status;
}