2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <NativeReal.h>
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,
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};
19 #define INFINITY (1.0/0.0)
24 static const double double_values[] = {
25 -M_E, M_E, -M_PI, M_PI, /* Better precision than with floats */
28 -2251799813685248.0, 2251799813685248.0,
30 -4503599627370496.0, 4503599627370496.0,
32 -1267650600228229401496703205376.0, 1267650600228229401496703205376.0,
36 -DBL_TRUE_MIN, DBL_TRUE_MIN
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,
44 -FLT_TRUE_MIN, FLT_TRUE_MIN,
46 INFINITY, -INFINITY, NAN
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]);
55 if(max_length == 0) return result_skipped;
57 if(float_set_size == sizeof(double) && asn_random_between(0, 1) == 0) {
58 d = double_values[asn_random_between(0, n_doubles - 1)];
60 d = float_values[asn_random_between(0, n_floats - 1)];
63 if(NativeReal__set(td, sptr, d) < 0) {
67 result_ok.length = float_set_size;