aboutsummaryrefslogtreecommitdiffstats
path: root/asn1c/tests/check-134.-gen-PER.c
blob: d129fe3b0487bf850d590bc723f6c455f1e6564f (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
/*
 * Verify INTEGER values with greater than 32 bits range.
 */
#undef	NDEBUG
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>

#include <T.h>

#ifndef  _LP64
int main() {
    assert(sizeof(void *) < 8);
    return;
}
#else   /* 64-bit platform */

static unsigned long i2ul(const INTEGER_t *i) {
    unsigned long l;
    int ret = asn_INTEGER2ulong(i, &l);
    assert(ret == 0);
    return l;
}

static void ul2i(INTEGER_t *i, unsigned long l) {
    int ret = asn_ulong2INTEGER(i, l);
    assert(ret == 0);
}

static void
verify(int testNo, T_t *ti) {
	asn_enc_rval_t er;
	asn_dec_rval_t rv;
	unsigned char buf[16];
	T_t *to = 0;

	fprintf(stderr, "%d IN: { %lu, %lu }\n", testNo,
		i2ul(&ti->unsigned33), i2ul(&ti->unsigned42));

	er = uper_encode_to_buffer(&asn_DEF_T, ti, buf, sizeof buf);
	assert(er.encoded == 33 + 42);

	rv = uper_decode(0, &asn_DEF_T, (void *)&to, buf, sizeof buf, 0, 0);
	assert(rv.code == RC_OK);

	fprintf(stderr, "%d ENC: %2x%2x%2x%2x %2x%2x%2x%2x\n", testNo,
		buf[0], buf[1], buf[2], buf[3],
		buf[4], buf[5], buf[6], buf[7]);
	fprintf(stderr, "%d OUT: { %lu, %lu } vs { %lu, %lu }\n",
		testNo,
		i2ul(&ti->unsigned33), i2ul(&ti->unsigned42),
		i2ul(&to->unsigned33), i2ul(&to->unsigned42));
	assert(i2ul(&ti->unsigned33) == i2ul(&to->unsigned33));
	assert(i2ul(&ti->unsigned42) == i2ul(&to->unsigned42));

	xer_fprint(stderr, &asn_DEF_T, ti);
	xer_fprint(stderr, &asn_DEF_T, to);
}

static void
NO_encode(int testNo, T_t *ti) {
	asn_enc_rval_t er;
	unsigned char buf[16];

	fprintf(stderr, "%d IN: { %lu, %lu }\n", testNo,
		i2ul(&ti->unsigned33), i2ul(&ti->unsigned42));

	er = uper_encode_to_buffer(&asn_DEF_T, ti, buf, sizeof buf);
	assert(er.encoded == -1);
}

int main() {
	T_t ti;

    memset(&ti, 0, sizeof(ti));
    ul2i(&ti.unsigned33, 0);
    ul2i(&ti.unsigned42, 0);
	verify(1, &ti);

    ul2i(&ti.unsigned33, 1);
    ul2i(&ti.unsigned42, 1);
	verify(2, &ti);

    ul2i(&ti.unsigned33, 5000000000);
    ul2i(&ti.unsigned42, 3153600000000);
	verify(3, &ti);

    ul2i(&ti.unsigned33, -1);
    ul2i(&ti.unsigned42, 0);
	NO_encode(4, &ti);

    ul2i(&ti.unsigned33, 0);
    ul2i(&ti.unsigned42, -1);
	NO_encode(5, &ti);

    ul2i(&ti.unsigned33, 5000000000 + 1);
    ul2i(&ti.unsigned42, 0);
	NO_encode(6, &ti);

    ul2i(&ti.unsigned33, 0);
    ul2i(&ti.unsigned42, 3153600000000 + 1);
	NO_encode(7, &ti);

    ul2i(&ti.unsigned33, 5000000000 - 1);
    ul2i(&ti.unsigned42, 3153600000000 - 1);
	verify(8, &ti);

	return 0;
}

#endif  /* 64-bit platform */