File Coverage

lib/Acme/ExtUtils/XSOne/Test/Calculator/Memory.xs
Criterion Covered Total %
statement 16 16 100.0
branch 11 12 91.6
condition n/a
subroutine n/a
pod n/a
total 27 28 96.4


line stmt bran cond sub pod time code
1             /*
2             * Acme::ExtUtils::XSOne::Test::Calculator::Memory - Memory and history functions
3             *
4             * This module accesses the shared state defined in _header.xs
5             */
6              
7             /* Memory package helpers - need access to memory_slots */
8 10           static int mem_is_valid_slot(int slot) {
9 10 100         return (slot >= 0 && slot < MAX_MEMORY_SLOTS);
    100          
10             }
11              
12 2           static int mem_get_used_slots(void) {
13 2           int count = 0;
14 22 100         for (int i = 0; i < MAX_MEMORY_SLOTS; i++) {
15 20 100         if (memory_slots[i] != 0.0) count++;
16             }
17 2           return count;
18             }
19              
20 2           static double mem_sum_all(void) {
21 2           double sum = 0.0;
22 22 100         for (int i = 0; i < MAX_MEMORY_SLOTS; i++) {
23 20           sum += memory_slots[i];
24             }
25 2           return sum;
26             }
27              
28 4           static void mem_add_to_slot(int slot, double value) {
29 4 50         if (mem_is_valid_slot(slot)) {
30 4           memory_slots[slot] += value;
31             }
32 4           }
33              
34             MODULE = Acme::ExtUtils::XSOne::Test::Calculator PACKAGE = Acme::ExtUtils::XSOne::Test::Calculator::Memory
35              
36             PROTOTYPES: DISABLE
37              
38             int
39             store(slot, value)
40             int slot
41             double value
42             CODE:
43             RETVAL = store_memory(slot, value);
44             if (!RETVAL) {
45             warn("Invalid memory slot %d (valid: 0-%d)", slot, MAX_MEMORY_SLOTS - 1);
46             }
47             OUTPUT:
48             RETVAL
49              
50             double
51             recall(slot)
52             int slot
53             CODE:
54             if (slot < 0 || slot >= MAX_MEMORY_SLOTS) {
55             warn("Invalid memory slot %d (valid: 0-%d)", slot, MAX_MEMORY_SLOTS - 1);
56             RETVAL = 0.0;
57             } else {
58             RETVAL = recall_memory(slot);
59             }
60             OUTPUT:
61             RETVAL
62              
63             void
64             clear()
65             CODE:
66             clear_all_memory();
67              
68             double
69             ans()
70             CODE:
71             RETVAL = get_last_result();
72             OUTPUT:
73             RETVAL
74              
75             int
76             history_count()
77             CODE:
78             RETVAL = history_count;
79             OUTPUT:
80             RETVAL
81              
82             void
83             get_history_entry(index)
84             int index
85             PPCODE:
86             if (index < 0 || index >= history_count) {
87             croak("Invalid history index %d (valid: 0-%d)", index, history_count - 1);
88             }
89             EXTEND(SP, 4);
90             PUSHs(sv_2mortal(newSVpvf("%c", history[index].operation)));
91             PUSHs(sv_2mortal(newSVnv(history[index].operand1)));
92             PUSHs(sv_2mortal(newSVnv(history[index].operand2)));
93             PUSHs(sv_2mortal(newSVnv(history[index].result)));
94              
95             int
96             max_memory_slots()
97             CODE:
98             RETVAL = MAX_MEMORY_SLOTS;
99             OUTPUT:
100             RETVAL
101              
102             int
103             max_history_entries()
104             CODE:
105             RETVAL = MAX_HISTORY;
106             OUTPUT:
107             RETVAL
108              
109             int
110             is_valid_slot(slot)
111             int slot
112             CODE:
113             RETVAL = mem_is_valid_slot(slot);
114             OUTPUT:
115             RETVAL
116              
117             int
118             used_slots()
119             CODE:
120             RETVAL = mem_get_used_slots();
121             OUTPUT:
122             RETVAL
123              
124             double
125             sum_all_slots()
126             CODE:
127             RETVAL = mem_sum_all();
128             OUTPUT:
129             RETVAL
130              
131             void
132             add_to(slot, value)
133             int slot
134             double value
135             CODE:
136             mem_add_to_slot(slot, value);
137              
138             void
139             import(...)
140             CODE:
141             {
142             static const char *memory_exports[] = {
143             "store", "recall", "clear", "ans",
144             "history_count", "get_history_entry",
145             "max_memory_slots", "max_history_entries",
146             "is_valid_slot", "used_slots", "sum_all_slots", "add_to"
147             };
148             do_import(aTHX_ "Acme::ExtUtils::XSOne::Test::Calculator::Memory",
149             memory_exports, 12, items, ax);
150             }