SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / REAL_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 <REAL.h>
8 #include <math.h>
9 #include <float.h>
10
11 asn_random_fill_result_t
12 REAL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
13                        const asn_encoding_constraints_t *constraints,
14                        size_t max_length) {
15     asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
16     asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
17     asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
18     static const double values[] = {
19         0, -0.0, -1, 1, -M_E, M_E, -3.14, 3.14, -M_PI, M_PI, -255, 255,
20         /* 2^51 */
21         -2251799813685248.0, 2251799813685248.0,
22         /* 2^52 */
23         -4503599627370496.0, 4503599627370496.0,
24         /* 2^100 */
25         -1267650600228229401496703205376.0, 1267650600228229401496703205376.0,
26         -FLT_MIN, FLT_MIN,
27         -FLT_MAX, FLT_MAX,
28         -DBL_MIN, DBL_MIN,
29         -DBL_MAX, DBL_MAX,
30 #ifdef  FLT_TRUE_MIN
31         -FLT_TRUE_MIN, FLT_TRUE_MIN,
32 #endif
33 #ifdef  DBL_TRUE_MIN
34         -DBL_TRUE_MIN, DBL_TRUE_MIN,
35 #endif
36         INFINITY, -INFINITY, NAN};
37     REAL_t *st;
38     double d;
39
40     (void)constraints;
41
42     if(max_length == 0) return result_skipped;
43
44     d = values[asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1)];
45
46     if(*sptr) {
47         st = *sptr;
48     } else {
49         st = (REAL_t*)(*sptr = CALLOC(1, sizeof(REAL_t)));
50         if(!st) {
51             return result_failed;
52         }
53     }
54
55     if(asn_double2REAL(st, d)) {
56         if(st == *sptr) {
57             ASN_STRUCT_RESET(*td, st);
58         } else {
59             ASN_STRUCT_FREE(*td, st);
60         }
61         return result_failed;
62     }
63
64     result_ok.length = st->size;
65     return result_ok;
66 }