SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / BOOLEAN_rfill.c
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <BOOLEAN.h>
8
9 asn_random_fill_result_t
10 BOOLEAN_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
11                     const asn_encoding_constraints_t *constraints,
12                     size_t max_length) {
13     asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
14     asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
15     asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
16     BOOLEAN_t *st = *sptr;
17
18     if(max_length == 0) return result_skipped;
19
20     if(st == NULL) {
21         st = (BOOLEAN_t *)(*sptr = CALLOC(1, sizeof(*st)));
22         if(st == NULL) {
23             return result_failed;
24         }
25     }
26
27 #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
28     if(!constraints || !constraints->per_constraints)
29         constraints = &td->encoding_constraints;
30     if(constraints->per_constraints) {
31         const asn_per_constraint_t *pc = &constraints->per_constraints->value;
32         if(pc->flags & APC_CONSTRAINED) {
33             *st = asn_random_between(pc->lower_bound, pc->upper_bound);
34             return result_ok;
35         }
36     }
37 #else
38     if(!constraints) constraints = &td->encoding_constraints;
39 #endif  /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
40
41     /* Simulate booleans that are sloppily set and biased. */
42     switch(asn_random_between(0, 7)) {
43     case 0:
44     case 1:
45     case 2:
46         *st = 0; break;
47     case 3: *st = -1; break;
48     case 4: *st = 1; break;
49     case 5: *st = INT_MIN; break;
50     case 6: *st = INT_MAX; break;
51     default:
52         *st = asn_random_between(INT_MIN, INT_MAX);
53         break;
54     }
55     return result_ok;
56 }