SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / NativeReal_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 <NativeReal.h>
8 #include <math.h>
9 #include <float.h>
10
11 asn_random_fill_result_t
12 NativeReal_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, 0};
16     asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
17     asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
18 #ifndef INFINITY
19 #define INFINITY (1.0/0.0)
20 #endif
21 #ifndef NAN
22 #define NAN (0.0/0.0)
23 #endif
24     static const double double_values[] = {
25         -M_E, M_E, -M_PI, M_PI, /* Better precision than with floats */
26         -1E+308, 1E+308,
27         /* 2^51 */
28         -2251799813685248.0, 2251799813685248.0,
29         /* 2^52 */
30         -4503599627370496.0, 4503599627370496.0,
31         /* 2^100 */
32         -1267650600228229401496703205376.0, 1267650600228229401496703205376.0,
33         -DBL_MIN, DBL_MIN,
34         -DBL_MAX, DBL_MAX,
35 #ifdef  DBL_TRUE_MIN
36         -DBL_TRUE_MIN, DBL_TRUE_MIN
37 #endif
38     };
39     static const float float_values[] = {
40         0, -0.0, -1, 1, -M_E, M_E, -3.14, 3.14, -M_PI, M_PI, -255, 255,
41         -FLT_MIN, FLT_MIN,
42         -FLT_MAX, FLT_MAX,
43 #ifdef  FLT_TRUE_MIN
44         -FLT_TRUE_MIN, FLT_TRUE_MIN,
45 #endif
46         INFINITY, -INFINITY, NAN
47     };
48     ssize_t float_set_size = NativeReal__float_size(td);
49     const size_t n_doubles = sizeof(double_values) / sizeof(double_values[0]);
50     const size_t n_floats = sizeof(float_values) / sizeof(float_values[0]);
51     double d;
52
53     (void)constraints;
54
55     if(max_length == 0) return result_skipped;
56
57     if(float_set_size == sizeof(double) && asn_random_between(0, 1) == 0) {
58         d = double_values[asn_random_between(0, n_doubles - 1)];
59     } else {
60         d = float_values[asn_random_between(0, n_floats - 1)];
61     }
62
63     if(NativeReal__set(td, sptr, d) < 0) {
64         return result_failed;
65     }
66
67     result_ok.length = float_set_size;
68     return result_ok;
69 }