~lifeeth/siam32/devel-beta

« back to all changes in this revision

Viewing changes to test/packet_test.c

  • Committer: Christopher D. Leary
  • Date: 2008-03-29 22:11:45 UTC
  • Revision ID: cdleary@gamma-20080329221145-nmku32vh5cxjx4xm
Ported packet.test and helpers.test over to new test output format -- still not sure why make_crc16 test is failling in helpers.test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <stdio.h>
2
2
 
 
3
#include "test.h"
3
4
#include "packet.h"
4
5
 
5
6
 
6
 
void
 
7
bool
7
8
bufferize_test()
8
9
{
9
10
    token_packet_t tp = token_packet_new(SETUP_PID, 0, 0);
10
 
    uint8_t data_payload[8] = { /* Get device descriptor. */
11
 
        0x80,
12
 
        0x06,
13
 
        0x00,
14
 
        0x01,
15
 
        0x00,
16
 
        0x00,
17
 
        0x12,
18
 
        0x00,
 
11
    uint8_t data_payload[] = { /* Get device descriptor. */
 
12
        0x80, 0x06, 0x00, 0x01, 0x00, 0x00, 0x12, 0x00,
19
13
    };
20
14
    data_packet_t dp = data_packet_new(DATA0_PID, data_payload, 64);
21
15
    uint8_t tp_buffer_bit_count = -1;
22
16
    uint8_t dp_buffer_bit_count = -1;
23
17
    uint8_t *tp_buffer = NULL;
24
18
    uint8_t *dp_buffer = NULL;
 
19
    bool passing = true;
25
20
 
26
 
    printf("DP payload bit count: %d\n",data_packet_get_payload_bit_count(dp));
 
21
    /* Data packet payload bit count. */
 
22
    {
 
23
        uint8_t found = data_packet_get_payload_bit_count(dp);
 
24
        uint8_t target = 64;
 
25
        bool verified = (found == target);
 
26
        passing = passing && verified;
 
27
        if (!verified)
 
28
            warn("Bad DP bit count; found: %d; target: %d\n",
 
29
                 found, target);
 
30
    }
27
31
 
28
32
    token_packet_recalculate_crc5(tp);
29
33
    data_packet_recalculate_crc16(dp);
30
34
 
31
35
    tp_buffer = token_packet_bufferize(tp, &tp_buffer_bit_count);
 
36
    dp_buffer = data_packet_bufferize(dp, &dp_buffer_bit_count);
 
37
    passing = passing && tp_buffer && dp_buffer;
32
38
    if (!tp_buffer)
33
39
        puts("TP buffer failed.");
34
 
    dp_buffer = data_packet_bufferize(dp, &dp_buffer_bit_count);
35
40
    if (!dp_buffer)
36
41
        puts("DP buffer failed.");
37
42
    sfree(tp_buffer);
38
43
    sfree(dp_buffer);
39
44
 
40
 
    printf("TP buffer bit count: %d\n", tp_buffer_bit_count);
41
 
    printf("DP buffer bit count: %d\n", dp_buffer_bit_count);
 
45
    /* Buffer bit counts. */
 
46
    {
 
47
        bool verified;
 
48
        uint8_t found, target;
 
49
 
 
50
        found = tp_buffer_bit_count;
 
51
        target = 24;
 
52
        verified = (found == target);
 
53
        passing = passing && verified;
 
54
        if (!verified)
 
55
            printf("Bad TP buffer bit count; found: %d; target: %d\n",
 
56
                   found, target);
 
57
 
 
58
        found = dp_buffer_bit_count;
 
59
        target = 88;
 
60
        verified = (found == target);
 
61
        passing = passing && verified;
 
62
        if (!verified)
 
63
            printf("Bad DP buffer bit count; found: %d; target: %d\n",
 
64
                   found, target);
 
65
    }
42
66
 
43
67
    token_packet_delete(tp);
44
68
    data_packet_delete(dp);
 
69
 
 
70
    return passing;
45
71
}
46
72
 
47
 
 
48
 
void
 
73
bool
49
74
crc5_test()
50
75
{
51
76
    uint8_t addresses[] = {0x15, 0x3a, 0x70};
52
77
    uint8_t endpoints[] = {0x0e, 0x0a, 0x04};
53
78
    uint8_t targets[] = {0x17, 0x1c, 0x0e};
54
79
    uint8_t iter;
 
80
    bool passing = true;
55
81
 
56
82
    for (iter = 0; iter < 3; iter++)
57
83
    {
58
 
        token_packet_t tp;
59
 
        bool verified;
 
84
        token_packet_t tp =
 
85
            token_packet_new(SETUP_PID, addresses[iter], endpoints[iter]);
60
86
        uint8_t found;
 
87
        bool verified;
61
88
 
62
 
        tp = token_packet_new(SETUP_PID, addresses[iter], endpoints[iter]);
63
89
        token_packet_recalculate_crc5(tp);
64
90
        found = token_packet_get_crc5(tp);
65
91
        verified = (found == targets[iter]);
66
 
        printf("CRC5 verified: %d; found: %d; target: %d\n",
67
 
               verified, found, targets[iter]);
 
92
        passing = passing && verified;
 
93
        if (!verified)
 
94
            warn("Bad CRC5: found: 0x%x; target: 0x%x\n",
 
95
                 found, targets[iter]);
68
96
        token_packet_delete(tp);
69
97
    }
 
98
 
 
99
    return passing;
70
100
}
71
101
 
72
 
 
73
 
void
 
102
bool
74
103
crc16_test()
75
104
{
76
 
    uint8_t sizes[] = {32, 32, 64, 64, 72};
77
105
    packet_id_t pids[] = {DATA0_PID, DATA1_PID, DATA1_PID, DATA0_PID};
78
106
    uint8_t payload0[] = {0x00, 0x01, 0x02, 0x03};
79
107
    uint8_t payload1[] = {0x23, 0x45, 0x67, 0x89};
80
108
    uint8_t payload2[] = {0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08};
81
109
    uint8_t payload3[] = {0x80, 0x06, 0x00, 0x01, 0x00, 0x00, 0x12, 0x00};
82
110
    unsigned char payload4[] = "123456789";
 
111
    uint8_t *payloads[] = {payload0, payload1, payload2, payload3, payload4};
 
112
    uint8_t payload_sizes[] = {32, 32, 64, 64, 72};
83
113
    uint16_t targets[] = {0xf75e, 0x7038, 0x88ee, 0x072f, 0x132d};
84
 
    data_packet_t dp;
85
 
    bool verified;
86
 
    uint16_t found;
87
 
 
88
 
    dp = data_packet_new(pids[0], payload0, sizes[0]);
89
 
    found = data_packet_get_crc16(dp);
90
 
    verified = (found == targets[0]);
91
 
    printf("CRC16 verified: %d; found: 0x%x; target: 0x%x\n", verified,
92
 
           found, targets[0]);
93
 
    data_packet_delete(dp);
94
 
 
95
 
    dp = data_packet_new(pids[1], payload1, sizes[1]);
96
 
    found = data_packet_get_crc16(dp);
97
 
    verified = (found == targets[1]);
98
 
    printf("CRC16 verified: %d; found: 0x%x; target: 0x%x\n", verified,
99
 
           found, targets[1]);
100
 
    data_packet_delete(dp);
101
 
 
102
 
    dp = data_packet_new(pids[2], payload2, sizes[2]);
103
 
    found = data_packet_get_crc16(dp);
104
 
    verified = (found == targets[2]);
105
 
    printf("CRC16 verified: %d; found: 0x%x; target: 0x%x\n", verified,
106
 
           found, targets[2]);
107
 
    data_packet_delete(dp);
108
 
 
109
 
    dp = data_packet_new(pids[3], payload3, sizes[3]);
110
 
    found = data_packet_get_crc16(dp);
111
 
    verified = (found == targets[3]);
112
 
    printf("CRC16 verified: %d; found: 0x%x; target: 0x%x\n", verified,
113
 
           found, targets[3]);
114
 
    data_packet_delete(dp);
115
 
 
116
 
    dp = data_packet_new(pids[4], payload4, sizes[4]);
117
 
    found = data_packet_get_crc16(dp);
118
 
    verified = (found == targets[4]);
119
 
    printf("CRC16 verified: %d; found: 0x%x; target: 0x%x\n", verified,
120
 
           found, targets[4]);
121
 
    data_packet_delete(dp);
 
114
    bool passing = true;
 
115
    uint8_t iter;
 
116
 
 
117
    for (iter = 0; iter < 5; iter++)
 
118
    {
 
119
        uint8_t *payload = payloads[iter];
 
120
        data_packet_t dp =
 
121
            data_packet_new(pids[iter], payload, payload_sizes[iter]);
 
122
        uint16_t found = data_packet_get_crc16(dp);
 
123
        bool verified = (found == targets[iter]);
 
124
        passing = passing && verified;
 
125
        if (!verified)
 
126
            warn("Bad CRC16: found: 0x%x; target: 0x%x\n",
 
127
                 found, targets[iter]);
 
128
        data_packet_delete(dp);
 
129
    }
 
130
 
 
131
    return passing;
122
132
}
123
133
 
124
 
 
125
134
int
126
135
main()
127
136
{
128
 
    bufferize_test();
129
 
    crc5_test();
130
 
    crc16_test();
131
 
 
 
137
    TEST(bufferize_test);
 
138
    TEST(crc5_test);
 
139
    TEST(crc16_test);
132
140
    return 0;
133
141
}