Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bda19cf

Browse files
Now StampJSON works
1 parent 5db502b commit bda19cf

File tree

1 file changed

+49
-38
lines changed

1 file changed

+49
-38
lines changed

t/321-galley-recursion-experiments_2.cpp

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232

3333
#include "test-chars-stamps.h"
3434

35+
// tmp
36+
#include <fstream>
37+
3538
using namespace TAP;
3639

3740
char short_sample[]="1234567";
@@ -54,7 +57,7 @@ class PoolPickerStamp : public virtual StampBaseStr
5457

5558
public:
5659
PoolPickerStamp(std::vector<std::shared_ptr<StampBaseStr>> new_pool);
57-
~PoolPickerStamp() {fprintf(stderr, "DESTROY!\n");};
60+
// ~PoolPickerStamp() {fprintf(stderr, "DESTROY!\n");};
5861

5962
std::string ExtractStr(std::shared_ptr<Blob> blob) override;
6063
virtual void add_weak(std::shared_ptr<StampBaseStr> stamp);
@@ -80,9 +83,9 @@ PoolPickerStamp::isRecursive()
8083
if(is_recursive || is_in_recursion)
8184
return true;
8285
is_in_recursion = true;
83-
for(auto stamp : pool)
86+
for(auto stamp : weak_pool)
8487
{
85-
if (stamp->isRecursive())
88+
if (stamp.lock()->isRecursive())
8689
{
8790
is_recursive = true; // Once recursive -- recursive forever.
8891
is_in_recursion = false;
@@ -100,13 +103,22 @@ PoolPickerStamp::ExtractStr(std::shared_ptr<Blob> blob)
100103
ORACLE_TYPE oracle = stamp_oracle.ExtractValue(blob);
101104

102105
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)
104109
{
105-
if (stamp.lock()->minSize() <= blob->Size())
110+
auto stamp = stamp_w.lock();
111+
if (stamp->minSize() <= blob->Size())
106112
{
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+
}
108118
}
109119
}
120+
if (unbounded_pool.size()>0)
121+
target_pool = unbounded_pool;
110122

111123
size_t index = OracleProportion(oracle, 0, target_pool.size() - 1);
112124
return target_pool[index].lock()->ExtractStr(blob);
@@ -115,20 +127,22 @@ PoolPickerStamp::ExtractStr(std::shared_ptr<Blob> blob)
115127
int
116128
PoolPickerStamp::minSize()
117129
{
118-
int res = INT_MAX;
130+
int res = INT_MAX / 2;
119131
/* Do not check is_recursive here: even if stamp is known to be recursive we
120132
* still should iterate all his non-recursive children to find real minimal
121133
* size */
122134
if (is_in_recursion)
123135
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)
126138
{
127-
int candidat = stamp->minSize();
139+
int candidat = stamp.lock()->minSize();
128140
if (res > candidat)
129141
res = candidat;
130142
}
131-
is_in_recursion = 0;
143+
is_in_recursion = false;
144+
if (res == INT_MAX / 2)
145+
return INT_MAX / 2;
132146
res += ORACLE_SIZE;
133147
return res;
134148
}
@@ -139,16 +153,19 @@ PoolPickerStamp::maxSize()
139153
int res = 0;
140154
if (is_recursive || is_in_recursion)
141155
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)
144158
{
145-
int candidat = stamp->maxSize();
159+
int candidat = stamp.lock()->maxSize();
146160
if (candidat == -1)
161+
{
162+
is_in_recursion = false;
147163
return -1;
164+
}
148165
if (res < candidat)
149166
res = candidat;
150167
}
151-
is_in_recursion = 0;
168+
is_in_recursion = false;
152169
res += ORACLE_SIZE;
153170
return res;
154171
}
@@ -172,6 +189,9 @@ class StampJSONString : public virtual StampDictT<DictLCAlphaSmall>
172189
protected:
173190
public:
174191
std::string ExtractStr(std::shared_ptr<Blob> blob) override;
192+
virtual int minSize() override {return 8;};
193+
virtual int maxSize() override {return 8;};
194+
175195
};
176196

177197

@@ -250,6 +270,7 @@ StampJSON::StampJSON()
250270
add_weak(stamp_i);
251271
add_weak(stamp_f);
252272
add_weak(stamp_s);
273+
253274
add_weak(stamp_a);
254275
add_weak(stamp_h);
255276
}
@@ -258,37 +279,27 @@ StampJSON::StampJSON()
258279
int
259280
main()
260281
{
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";
271286

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());
274289

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());
276292

277-
278-
auto blob = std::make_shared<Blob>((char *)bin_sample, strlen((char *)bin_sample));
279293

280294
auto stamp_j = std::make_shared<StampJSON>();
281295

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());
287297

288-
fprintf(stderr,"%i %s\n",picker->isRecursive(), s.c_str());
289-
}
298+
std::string s = stamp_j->ExtractStr(blob);
290299

300+
printf("%s\n", s.c_str());
291301

302+
/*
292303
293304
TEST_START(6);
294305
{
@@ -300,5 +311,5 @@ main()
300311
is(OracleProportion(65535-256,0,255), 254);
301312
302313
}
303-
TEST_END;
314+
TEST_END;*/
304315
}

0 commit comments

Comments
 (0)