32
32
33
33
#include " test-chars-stamps.h"
34
34
35
+ // tmp
36
+ #include < fstream>
37
+
35
38
using namespace TAP ;
36
39
37
40
char short_sample[]=" 1234567" ;
@@ -54,7 +57,7 @@ class PoolPickerStamp : public virtual StampBaseStr
54
57
55
58
public:
56
59
PoolPickerStamp (std::vector<std::shared_ptr<StampBaseStr>> new_pool);
57
- ~PoolPickerStamp () {fprintf (stderr," DESTROY!\n " );};
60
+ // ~PoolPickerStamp() {fprintf(stderr, "DESTROY!\n");};
58
61
59
62
std::stringExtractStr (std::shared_ptr<Blob> blob)override ;
60
63
virtual void add_weak (std::shared_ptr<StampBaseStr> stamp);
@@ -80,9 +83,9 @@ PoolPickerStamp::isRecursive()
80
83
if (is_recursive || is_in_recursion)
81
84
return true ;
82
85
is_in_recursion =true ;
83
- for (auto stamp :pool )
86
+ for (auto stamp :weak_pool )
84
87
{
85
- if (stamp->isRecursive ())
88
+ if (stamp. lock () ->isRecursive ())
86
89
{
87
90
is_recursive =true ;// Once recursive -- recursive forever.
88
91
is_in_recursion =false ;
@@ -100,13 +103,22 @@ PoolPickerStamp::ExtractStr(std::shared_ptr<Blob> blob)
100
103
ORACLE_TYPE oracle = stamp_oracle.ExtractValue (blob);
101
104
102
105
std::vector<std::weak_ptr<StampBaseStr>> target_pool;
103
- for (auto stamp : weak_pool)
106
+ std::vector<std::weak_ptr<StampBaseStr>> unbounded_pool;
107
+
108
+ for (auto stamp_w : weak_pool)
104
109
{
105
- if (stamp.lock ()->minSize () <= blob->Size ())
110
+ auto stamp = stamp_w.lock ();
111
+ if (stamp->minSize () <= blob->Size ())
106
112
{
107
- target_pool.push_back (stamp);
113
+ target_pool.push_back (stamp_w);
114
+ if (stamp->maxSize () == -1 || stamp->maxSize () >= blob->Size ())
115
+ {
116
+ unbounded_pool.push_back (stamp_w);
117
+ }
108
118
}
109
119
}
120
+ if (unbounded_pool.size ()>0 )
121
+ target_pool = unbounded_pool;
110
122
111
123
size_t index =OracleProportion (oracle,0 , target_pool.size () -1 );
112
124
return target_pool[index ].lock ()->ExtractStr (blob);
@@ -115,20 +127,22 @@ PoolPickerStamp::ExtractStr(std::shared_ptr<Blob> blob)
115
127
int
116
128
PoolPickerStamp::minSize ()
117
129
{
118
- int res = INT_MAX;
130
+ int res = INT_MAX / 2 ;
119
131
/* Do not check is_recursive here: even if stamp is known to be recursive we
120
132
* still should iterate all his non-recursive children to find real minimal
121
133
* size*/
122
134
if (is_in_recursion)
123
135
return res;
124
- is_in_recursion =1 ;/* Do not use isRecursive() inside as it uses same flag*/
125
- for (auto stamp :pool )
136
+ is_in_recursion =true ;/* Do not use isRecursive() inside as it uses same flag*/
137
+ for (auto stamp :weak_pool )
126
138
{
127
- int candidat = stamp->minSize ();
139
+ int candidat = stamp. lock () ->minSize ();
128
140
if (res > candidat)
129
141
res = candidat;
130
142
}
131
- is_in_recursion =0 ;
143
+ is_in_recursion =false ;
144
+ if (res == INT_MAX /2 )
145
+ return INT_MAX /2 ;
132
146
res += ORACLE_SIZE;
133
147
return res;
134
148
}
@@ -139,16 +153,19 @@ PoolPickerStamp::maxSize()
139
153
int res =0 ;
140
154
if (is_recursive || is_in_recursion)
141
155
return -1 ;
142
- is_in_recursion =1 ;/* Do not use isRecursive() inside as it uses same flag*/
143
- for (auto stamp :pool )
156
+ is_in_recursion =true ;/* Do not use isRecursive() inside as it uses same flag*/
157
+ for (auto stamp :weak_pool )
144
158
{
145
- int candidat = stamp->maxSize ();
159
+ int candidat = stamp. lock () ->maxSize ();
146
160
if (candidat == -1 )
161
+ {
162
+ is_in_recursion =false ;
147
163
return -1 ;
164
+ }
148
165
if (res < candidat)
149
166
res = candidat;
150
167
}
151
- is_in_recursion =0 ;
168
+ is_in_recursion =false ;
152
169
res += ORACLE_SIZE;
153
170
return res;
154
171
}
@@ -172,6 +189,9 @@ class StampJSONString : public virtual StampDictT<DictLCAlphaSmall>
172
189
protected:
173
190
public:
174
191
std::stringExtractStr (std::shared_ptr<Blob> blob)override ;
192
+ virtual int minSize ()override {return 8 ;};
193
+ virtual int maxSize ()override {return 8 ;};
194
+
175
195
};
176
196
177
197
@@ -250,6 +270,7 @@ StampJSON::StampJSON()
250
270
add_weak (stamp_i);
251
271
add_weak (stamp_f);
252
272
add_weak (stamp_s);
273
+
253
274
add_weak (stamp_a);
254
275
add_weak (stamp_h);
255
276
}
@@ -258,37 +279,27 @@ StampJSON::StampJSON()
258
279
int
259
280
main ()
260
281
{
261
- auto stamp_d = std::make_shared<StampJSONString>();
262
- auto stamp_i = std::make_shared<StampJSONInt>();
263
- auto stamp_f = std::make_shared<StampJSONFloat>();
264
-
265
-
266
- // PoolPickerStamp stamp({stamp_i, stamp_f, stamp_d});
267
- std::shared_ptr<PoolPickerStamp>picker (new PoolPickerStamp ({stamp_f, stamp_i, stamp_d}));
268
- // picker->add_weak(picker);
269
- auto stamp_a = std::make_shared<StampJSONArray>(picker);
270
- picker->add_weak (stamp_a);
282
+ // std::fstream f{"/dev/random"};
283
+ std::fstream f{" buf" };
284
+ if (!f)
285
+ std::cerr <<" Unable to open file" ;
271
286
272
- auto stamp_h = std::make_shared<StampJSONHash>(picker );
273
- picker-> add_weak (stamp_h );
287
+ std::vector< char > buffer ( 128 , 0 );
288
+ f. read (&buffer[ 0 ], buffer. size () );
274
289
275
- fprintf (stderr," hash sizes= %i %i\n " ,stamp_h->minSize (), stamp_h->maxSize ());
290
+ // auto blob = std::make_shared<Blob>((char *)bin_sample, strlen((char *)bin_sample));
291
+ auto blob = std::make_shared<Blob>(&buffer[0 ], buffer.size ());
276
292
277
-
278
- auto blob = std::make_shared<Blob>((char *)bin_sample,strlen ((char *)bin_sample));
279
293
280
294
auto stamp_j = std::make_shared<StampJSON>();
281
295
282
- fprintf (stderr," %i %i\n " ,stamp_a->minSize (), stamp_a->maxSize ());
283
- // for(int i =0; i<25; i++)
284
- {
285
- // std::string s = stamp_a->ExtractStr(blob);
286
- std::string s = stamp_h->ExtractStr (blob);
296
+ // printf("%i\n", stamp_j->minSize());
287
297
288
- fprintf (stderr," %i %s\n " ,picker->isRecursive (), s.c_str ());
289
- }
298
+ std::string s = stamp_j->ExtractStr (blob);
290
299
300
+ printf (" %s\n " , s.c_str ());
291
301
302
+ /*
292
303
293
304
TEST_START(6);
294
305
{
@@ -300,5 +311,5 @@ main()
300
311
is(OracleProportion(65535-256,0,255), 254);
301
312
302
313
}
303
- TEST_END;
314
+ TEST_END;*/
304
315
}