~ubuntu-branches/ubuntu/trusty/geis/trusty

« back to all changes in this revision

Viewing changes to testsuite/libgeis/check_device.c

  • Committer: Package Import Robot
  • Author(s): Chase Douglas
  • Date: 2012-07-30 08:51:42 UTC
  • Revision ID: package-import@ubuntu.com-20120730085142-jrc33ygjvt0ob1wl
Tags: upstream-2.2.11
ImportĀ upstreamĀ versionĀ 2.2.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * unit tests for the geis device module
 
3
 */
 
4
#include <check.h>
 
5
 
 
6
#include "libgeis/geis_device.h"
 
7
#include "libgeis/geis_attr.h"
 
8
#include <stdio.h>
 
9
 
 
10
 
 
11
/* fixtures */
 
12
static GeisDeviceBag g_device_bag;
 
13
static GeisString    g_device_name = "device";
 
14
static GeisInteger   g_device_id   = 12;
 
15
 
 
16
/* fixture setup */
 
17
static void
 
18
construct_bag()
 
19
{
 
20
  g_device_bag = geis_device_bag_new();
 
21
}
 
22
 
 
23
/* fixture teardown */
 
24
static void
 
25
destroy_bag()
 
26
{
 
27
  geis_device_bag_delete(g_device_bag);
 
28
}
 
29
 
 
30
/* verify bag construction/destruction */
 
31
START_TEST(construct)
 
32
{
 
33
  GeisDeviceBag bag = geis_device_bag_new();
 
34
  fail_unless(bag != NULL, "failed to create device bag");
 
35
  geis_device_bag_delete(bag);
 
36
}
 
37
END_TEST
 
38
 
 
39
/* verify bag insertion */
 
40
START_TEST(insert_device)
 
41
{
 
42
  GeisDevice device = geis_device_new(g_device_name, g_device_id);
 
43
  geis_device_bag_insert(g_device_bag, device);
 
44
  fail_unless(geis_device_bag_count(g_device_bag) == 1,
 
45
              "unexpected bag size after insertion");
 
46
}
 
47
END_TEST
 
48
 
 
49
/* verify bag removal */
 
50
START_TEST(remove_device)
 
51
{
 
52
  GeisDevice device = geis_device_new(g_device_name, g_device_id);
 
53
  geis_device_bag_insert(g_device_bag, device);
 
54
  fail_unless(geis_device_bag_count(g_device_bag) == 1,
 
55
              "unexpected bag size after insertion");
 
56
  geis_device_bag_remove(g_device_bag, device);
 
57
  fail_unless(geis_device_bag_count(g_device_bag) == 0,
 
58
              "unexpected bag size after removal");
 
59
}
 
60
END_TEST
 
61
 
 
62
 
 
63
/*
 
64
 * Verify retrieval by name works.
 
65
 */
 
66
START_TEST(attribute_name)
 
67
{
 
68
  GeisDevice device = geis_device_new(g_device_name, g_device_id);
 
69
  GeisAttr in_attr, out_attr;
 
70
  const char *attribute_name = "TEST_ATTRIBUTE";
 
71
  int attr_value = 60;
 
72
  fail_if(geis_device_attr_by_name(device, attribute_name) != NULL);
 
73
  in_attr = geis_attr_new(attribute_name, GEIS_ATTR_TYPE_INTEGER, (void*)&attr_value);
 
74
  geis_device_add_attr(device, in_attr);
 
75
  out_attr = geis_device_attr_by_name(device, attribute_name);
 
76
  fail_if(out_attr == NULL);
 
77
  fail_if(geis_attr_value_to_integer(out_attr) != attr_value);
 
78
}
 
79
END_TEST
 
80
 
 
81
START_TEST(expand)
 
82
{
 
83
  GeisSize i;
 
84
  for (i = 0; i < 24; ++i)
 
85
  {
 
86
    GeisSize count;
 
87
    char name[32];
 
88
    sprintf(name, "%04zu", i);
 
89
    GeisDevice device = geis_device_new(name, i);
 
90
    geis_device_bag_insert(g_device_bag, device);
 
91
    count = geis_device_bag_count(g_device_bag);
 
92
    fail_unless(count == (i+1),
 
93
                "unexpected bag size %ld after insertion, expected %d",
 
94
                count, i+1);
 
95
  }
 
96
}
 
97
END_TEST
 
98
 
 
99
 
 
100
 
 
101
/* boilerplate */
 
102
Suite *
 
103
make_device_suite()
 
104
{
 
105
  Suite *s = suite_create("geis2-device");
 
106
 
 
107
  TCase *create = tcase_create("device-bag-creation");
 
108
  tcase_add_test(create, construct);
 
109
  suite_add_tcase(s, create);
 
110
 
 
111
  TCase *usage = tcase_create("device-bag-usage");
 
112
  tcase_add_checked_fixture(usage, construct_bag, destroy_bag);
 
113
  tcase_add_test(usage, insert_device);
 
114
  tcase_add_test(usage, remove_device);
 
115
  tcase_add_test(usage, expand);
 
116
  suite_add_tcase(s, usage);
 
117
 
 
118
  TCase *dev_attr = tcase_create("device-attributes");
 
119
  tcase_add_test(usage, attribute_name);
 
120
  suite_add_tcase(s, dev_attr);
 
121
  return s;
 
122
}
 
123