sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
|
sl@0
|
20 |
|
sl@0
|
21 |
#include <liboil/liboil.h>
|
sl@0
|
22 |
#include <liboil/liboildebug.h>
|
sl@0
|
23 |
#include <time.h>
|
sl@0
|
24 |
#include <stdio.h>
|
sl@0
|
25 |
#include <string.h>
|
sl@0
|
26 |
|
sl@0
|
27 |
#include <liboil/globals.h>
|
sl@0
|
28 |
|
sl@0
|
29 |
#define LOG_FILE "c:\\logs\\examples_oil-random_log.txt"
|
sl@0
|
30 |
#include "std_log_result.h"
|
sl@0
|
31 |
#define LOG_FILENAME_LINE __FILE__, __LINE__
|
sl@0
|
32 |
|
sl@0
|
33 |
void create_xml(int result)
|
sl@0
|
34 |
{
|
sl@0
|
35 |
if(result)
|
sl@0
|
36 |
assert_failed = 1;
|
sl@0
|
37 |
|
sl@0
|
38 |
testResultXml("examples_oil-random");
|
sl@0
|
39 |
close_log_file();
|
sl@0
|
40 |
}
|
sl@0
|
41 |
typedef struct _OilRandomState OilRandomState;
|
sl@0
|
42 |
|
sl@0
|
43 |
struct _OilRandomState {
|
sl@0
|
44 |
int index;
|
sl@0
|
45 |
uint32_t mt[624];
|
sl@0
|
46 |
uint32_t bits[624];
|
sl@0
|
47 |
};
|
sl@0
|
48 |
|
sl@0
|
49 |
OilRandomState state;
|
sl@0
|
50 |
|
sl@0
|
51 |
static void _oil_random_init (void);
|
sl@0
|
52 |
uint32_t oil_random_get_int (OilRandomState *state);
|
sl@0
|
53 |
void oil_random_get_bits (OilRandomState *state, uint8_t *dest, int n);
|
sl@0
|
54 |
|
sl@0
|
55 |
int main (int argc, char *argv[])
|
sl@0
|
56 |
{
|
sl@0
|
57 |
int i;
|
sl@0
|
58 |
uint32_t a[10];
|
sl@0
|
59 |
|
sl@0
|
60 |
oil_init();
|
sl@0
|
61 |
|
sl@0
|
62 |
_oil_random_init();
|
sl@0
|
63 |
|
sl@0
|
64 |
for(i=0;i<10;i++){
|
sl@0
|
65 |
std_log(LOG_FILENAME_LINE,"%d\n", oil_random_get_int(&state));
|
sl@0
|
66 |
}
|
sl@0
|
67 |
|
sl@0
|
68 |
oil_random_get_bits (&state, (void *)a, 10*4);
|
sl@0
|
69 |
for(i=0;i<10;i++){
|
sl@0
|
70 |
std_log(LOG_FILENAME_LINE,"%d\n", a[i]);
|
sl@0
|
71 |
}
|
sl@0
|
72 |
if(assert_failed)
|
sl@0
|
73 |
std_log(LOG_FILENAME_LINE,"Test Fail");
|
sl@0
|
74 |
else
|
sl@0
|
75 |
std_log(LOG_FILENAME_LINE,"Test Successful");
|
sl@0
|
76 |
create_xml(0);
|
sl@0
|
77 |
|
sl@0
|
78 |
}
|
sl@0
|
79 |
|
sl@0
|
80 |
|
sl@0
|
81 |
void
|
sl@0
|
82 |
oil_random_state_seed (OilRandomState *state, uint32_t seed)
|
sl@0
|
83 |
{
|
sl@0
|
84 |
uint32_t *mt = state->mt;
|
sl@0
|
85 |
int i;
|
sl@0
|
86 |
|
sl@0
|
87 |
mt[0] = seed;
|
sl@0
|
88 |
for(i=1;i<624;i++) {
|
sl@0
|
89 |
mt[i] = (1812433253UL * (mt[i-1] ^ (mt[i-1] >> 30)) + i);
|
sl@0
|
90 |
}
|
sl@0
|
91 |
oil_mt19937 (state->bits, state->mt);
|
sl@0
|
92 |
state->index = 0;
|
sl@0
|
93 |
}
|
sl@0
|
94 |
|
sl@0
|
95 |
|
sl@0
|
96 |
|
sl@0
|
97 |
static void
|
sl@0
|
98 |
_oil_random_init (void)
|
sl@0
|
99 |
{
|
sl@0
|
100 |
int seed;
|
sl@0
|
101 |
|
sl@0
|
102 |
seed = time(NULL);
|
sl@0
|
103 |
OIL_ERROR("seed is %d", seed);
|
sl@0
|
104 |
|
sl@0
|
105 |
oil_random_state_seed (&state, seed);
|
sl@0
|
106 |
}
|
sl@0
|
107 |
|
sl@0
|
108 |
uint32_t
|
sl@0
|
109 |
oil_random_get_int (OilRandomState *state)
|
sl@0
|
110 |
{
|
sl@0
|
111 |
if (state->index >= 624) {
|
sl@0
|
112 |
oil_mt19937 (state->bits, state->mt);
|
sl@0
|
113 |
state->index = 0;
|
sl@0
|
114 |
}
|
sl@0
|
115 |
return state->bits[state->index++];
|
sl@0
|
116 |
}
|
sl@0
|
117 |
|
sl@0
|
118 |
void
|
sl@0
|
119 |
oil_random_get_bits (OilRandomState *state, uint8_t *dest, int n)
|
sl@0
|
120 |
{
|
sl@0
|
121 |
int i = state->index * 4;
|
sl@0
|
122 |
int m;
|
sl@0
|
123 |
|
sl@0
|
124 |
while(n > 0) {
|
sl@0
|
125 |
if (i >= 624*4) {
|
sl@0
|
126 |
oil_mt19937 (state->bits, state->mt);
|
sl@0
|
127 |
i = 0;
|
sl@0
|
128 |
}
|
sl@0
|
129 |
|
sl@0
|
130 |
m = n;
|
sl@0
|
131 |
if (m > 624*4 - i) m = 624*4 - i;
|
sl@0
|
132 |
|
sl@0
|
133 |
memcpy (dest, ((uint8_t *)state->bits) + i, m);
|
sl@0
|
134 |
i += m;
|
sl@0
|
135 |
n -= m;
|
sl@0
|
136 |
}
|
sl@0
|
137 |
state->index = (i+3)/4;
|
sl@0
|
138 |
}
|
sl@0
|
139 |
|
sl@0
|
140 |
|