Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita6d6c1b

Browse files
Add ExtractValue method to arithmetic type stamp
1 parentb96516a commita6d6c1b

File tree

5 files changed

+171
-148
lines changed

5 files changed

+171
-148
lines changed

‎blobstamper/galley.cpp‎

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,21 @@ GalleySeries::extract_internal(Blob &blob)
9595

9696
/* Getting count oracle and normalze it to fit available size*/
9797
size_t count_max = (blob.Size() - ORACLE_SIZE) / (stamp.minSize() + ORACLE_SIZE);//First oracle - for number of items, and second one is oracle for each item size
98-
ORACLE_STAMP stamp_oracle;
99-
ORACLE_TYPE *count_oracle;
10098

101-
std::vector<char> v = blob.ShiftSingleStampBin(stamp_oracle);
102-
count_oracle =(ORACLE_TYPE *) &v[0];
99+
ORACLE_STAMPstamp_oracle;
100+
ORACLE_TYPEcount_oracle =stamp_oracle.ExtractValue(blob);
103101

104-
ORACLE_TYPE count_target = count_max *(*count_oracle) / ORACLE_MAX +1;/* +1 -- это грубая эмуляция округления вверх. oracle == ORACLE_MAX-1 == 65534 должен дать count_max*/
102+
ORACLE_TYPE count_target = count_max * count_oracle / ORACLE_MAX +1;/* +1 -- это грубая эмуляция округления вверх. oracle == ORACLE_MAX-1 == 65534 должен дать count_max*/
105103
if (count_target > count_max) count_target = count_max;// В случае если oracle оказался рваен ORACLE_MAX
106104

107105
/* Getting size oracles for each part*/
108106
std::vector<ORACLE_TYPE> size_oracles;
109107
int size_oracle_total =0;
110108
for(int i =0; i<count_target; i++)
111109
{
112-
std::vector<char> v = blob.ShiftSingleStampBin(stamp_oracle);
113-
ORACLE_TYPE *o = (ORACLE_TYPE *) &v[0];
114-
size_oracles.push_back(*o);
115-
size_oracle_total += *o;
110+
ORACLE_TYPE o = stamp_oracle.ExtractValue(blob);
111+
size_oracles.push_back(o);
112+
size_oracle_total += o;
116113
}
117114

118115
/* Calculating available vairable size, that will be destributed between parts according to size oracles*/
@@ -142,9 +139,10 @@ GalleySeries::extract_internal(Blob &blob)
142139
{
143140
if(stamp.minSize() + stamp_oracle.minSize() > blob.Size())
144141
break;
145-
std::vector<char> v = blob.ShiftSingleStampBin(stamp_oracle);
146-
ORACLE_TYPE *oracle = (ORACLE_TYPE *) &v[0];
147-
int size = (double) *oracle / ORACLE_MAX * (var_size +1);/* +1 -- это грубая эмуляция округления вверх. oracle == ORACLE_MAX-1 == 65534 должен дать count_max*/
142+
143+
ORACLE_TYPE oracle = stamp_oracle.ExtractValue(blob);
144+
145+
int size = (double) oracle / ORACLE_MAX * (var_size +1);/* +1 -- это грубая эмуляция округления вверх. oracle == ORACLE_MAX-1 == 65534 должен дать count_max*/
148146
if (size > var_size) size = var_size;// In case we've hit oracle == ORACLE_MAX boundary
149147
size += fixed_size;
150148
Blob blob2 = blob.ShiftBytes(size);
@@ -216,10 +214,8 @@ GalleyVector::extract_internal(Blob &blob)
216214
/* try do devide available data between variated and unbounded stamps*/
217215
/* if predicted variated size is smaller than varited_total_size_limit we will decrice that limit*/
218216

219-
std::vector<char> v = blob.ShiftSingleStampBin(oracle_stamp);
220-
221-
ORACLE_TYPE * oracle = (ORACLE_TYPE *) &v[0];
222-
int predicted_variated_limit =round ((double) *oracle / (double) ORACLE_MAX * (double) (avaliable_nonfixed_size));
217+
ORACLE_TYPE oracle = oracle_stamp.ExtractValue(blob);
218+
int predicted_variated_limit =round ((double) oracle / (double) ORACLE_MAX * (double) (avaliable_nonfixed_size));
223219

224220
if (varited_total_size_limit > predicted_variated_limit)
225221
varited_total_size_limit = predicted_variated_limit;
@@ -242,9 +238,7 @@ GalleyVector::extract_internal(Blob &blob)
242238
modifier =1;//Nothing to predict, it will use all space
243239
}else
244240
{
245-
std::vector<char> v = blob.ShiftSingleStampBin(oracle_stamp);
246-
ORACLE_TYPE * oracle = (ORACLE_TYPE *) &v[0];
247-
o_value = * oracle;
241+
o_value = oracle_stamp.ExtractValue(blob);
248242
modifier = (double) o_value / (double) ORACLE_MAX;
249243
}
250244
if (s.isUnbounded())

‎blobstamper/stamp_arithm.h‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,34 @@ template<class T> class StampArithm: public StampFixed
99
public:
1010
StampArithm() { size =sizeof(T);};
1111
std::stringExtractStr(Blob &blob)override;
12+
TExtractValue(Blob &blob);
1213
};
1314

1415
template<classT> std::string
1516
StampArithm<T>::ExtractStr(Blob &blob)
1617
{
1718
std::string res;
18-
std::vector<char>bin =this->ExtractBin(blob);
19+
std::vector<char>v =this->ExtractBin(blob);
1920

20-
if (bin.size() ==0)
21+
if (v.size() ==0)
2122
return"";
2223

23-
T *pT = (T *) &bin[0];
24+
T *pT = (T *) &v[0];
2425

2526
returnto_string_precise(*pT);
2627
}
2728

29+
template<classT> T
30+
StampArithm<T>::ExtractValue(Blob &blob)
31+
{
32+
std::vector<char> v =ExtractBin(blob);
33+
if (v.size() ==0)
34+
{
35+
/* FIXME exeption should be here*/
36+
}
37+
T *pT = (T *) &v[0];
38+
return *pT;
39+
}
40+
41+
2842
#endif/* STAMP_ATOMIC_H*/

‎t/110-stamp-arithm.cpp‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#include<string.h>
2+
3+
#include<exception>
4+
#include<string>
5+
#include<cstdlib>
6+
7+
#defineWANT_TEST_EXTRAS
8+
#include<tap++/tap++.h>
9+
10+
#include"blobstamper/blobstamper.h"
11+
#include"blobstamper/helpers.h"
12+
13+
14+
usingnamespaceTAP;
15+
16+
17+
/* Tests for atomic type stamps*/
18+
19+
char sample_data_char[] ="Some string";
20+
shortint sample_data_int16[] = {1, -2, -3,4,555,66};
21+
int sample_data_int32[] = {10, -20, -30,40,500,6};
22+
longlong sample_data_int64[] = {100, -200, -300,400,5,6};
23+
double sample_data_double[] = {1.4142,2,3.1415,4.2e01,5,6,7};
24+
25+
int
26+
main()
27+
{
28+
size_t sample_data_int16_size =sizeof(sample_data_int16);
29+
size_t sample_data_int32_size =sizeof(sample_data_int32);
30+
size_t sample_data_int64_size =sizeof(sample_data_int64);
31+
size_t sample_data_double_size =sizeof(double) *7;
32+
33+
TEST_START(21);
34+
35+
/* Check that Bin and Str Char stamps works well*/
36+
{/* 1, 2, 3*/
37+
Blobblob(sample_data_char,strlen(sample_data_char));
38+
StampArithm<char> stamp;
39+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
40+
char * pc = (char *) &v[0];
41+
is(*pc,'S' ,"Bin Char stamp works well");
42+
43+
std::string s = blob.ShiftSingleStampStr(stamp);
44+
is(s,"111" ,"Str UInt8 stamp works well");// 'o'==111
45+
46+
char c = stamp.ExtractValue(blob);
47+
is(c,'m' ,"extract char as value works well");
48+
}
49+
50+
/* Check that Bin and Srt Int16 stamps works well*/
51+
{/* 4, 5, 6, 7, 8*/
52+
Blobblob((char *)sample_data_int16, sample_data_int16_size);
53+
StampArithm<shortint> stamp;
54+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
55+
shortint * pi = (shortint *) &v[0];
56+
is(*pi,1 ,"Bin Int16 stamp works well");
57+
58+
StampArithm<unsignedshortint> stamp_unsigned;
59+
std::string s = blob.ShiftSingleStampStr(stamp_unsigned);
60+
is(s,"65534" ,"Str UInt16 stamp works well");// (unsigned short int)-2 == 65534
61+
62+
StampArithm<signedshortint> stamp_signed;
63+
s = blob.ShiftSingleStampStr(stamp_signed);
64+
is(s,"-3" ,"Str SInt16 stamp works well");
65+
66+
unsignedshortint ui = stamp_unsigned.ExtractValue(blob);
67+
is(ui,4,"Extract unsigned int16 as value");
68+
69+
signedshortint si = stamp_signed.ExtractValue(blob);
70+
is(si,555,"Extract signed int16 as value");
71+
}
72+
73+
/* Check that Bin and Srt Int32 stamps works well*/
74+
{/* 9, 10, 11, 12, 13*/
75+
Blobblob((char *)sample_data_int32, sample_data_int32_size);
76+
StampArithm<int> stamp;
77+
78+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
79+
80+
int * i = (int *) &v[0];
81+
is(*i,10 ,"Bin Int32 stamp works well");
82+
83+
StampArithm<unsignedint> stamp_unsigned;
84+
std::string s = blob.ShiftSingleStampStr(stamp_unsigned);
85+
is(s,"4294967276" ,"Str UInt32 stamp works well");// (unsigned short int)-20 == 4294967276
86+
87+
StampArithm<signedint> stamp_signed;
88+
s = blob.ShiftSingleStampStr(stamp_signed);
89+
is(s,"-30" ,"Str SInt32 stamp works well");
90+
91+
unsignedint ui = stamp_unsigned.ExtractValue(blob);
92+
is(ui,40,"Extract unsigned int32 as value");
93+
94+
signedint si = stamp_signed.ExtractValue(blob);
95+
is(si,500,"Extract signed int32 as value");
96+
97+
}
98+
99+
100+
/* Check that Bin and Srt Int64 stamps works well*/
101+
{/* 14, 15, 16, 17, 18*/
102+
Blobblob((char *)sample_data_int64, sample_data_int64_size);
103+
StampArithm<longlong> stamp;
104+
105+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
106+
107+
longlong * i = (longlong *) &v[0];
108+
is(*i,100 ,"Bin Int64 stamp works well");
109+
110+
StampArithm<unsignedlonglong> stamp_unsigned;
111+
std::string s = blob.ShiftSingleStampStr(stamp_unsigned);
112+
is(s,"18446744073709551416" ,"Str UInt64 stamp works well");// (unsigned short int)-200 == 18446744073709551416
113+
114+
StampArithm<signedlonglong> stamp_signed;
115+
s = blob.ShiftSingleStampStr(stamp_signed);
116+
is(s,"-300" ,"Str SInt64 stamp works well");
117+
118+
unsignedlonglong ui = stamp_unsigned.ExtractValue(blob);
119+
is(ui,400,"Extract unsigned int32 as value");
120+
121+
signedlonglong si = stamp_signed.ExtractValue(blob);
122+
is(si,5,"Extract signed int32 as value");
123+
}
124+
125+
/* Test Double stamp*/
126+
{/* 19, 20, 21*/
127+
Blobblob((char *)sample_data_double, sample_data_double_size);
128+
StampArithm<double> stamp;
129+
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
130+
double *pd = (double *) &v[0];
131+
is(*pd,1.4142,"Bin Double stamp works well");
132+
133+
std::string res = blob.ShiftSingleStampStr(stamp);
134+
is(res,"2","Str Double stamp works well");
135+
136+
double d = stamp.ExtractValue(blob);
137+
is(d,3.1415,"Extract double as value");
138+
}
139+
TEST_END;
140+
}

‎t/110-stamp-atomic.cpp‎

Lines changed: 0 additions & 125 deletions
This file was deleted.

‎t/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LIBRARY_VAR := LD_LIBRARY_PATH
99
TEST_GOALS := ./00-sanity.t\
1010
./001-blob-generic.t\
1111
./100-stamp-base.t\
12-
./110-stamp-atomic.t\
12+
./110-stamp-arithm.t\
1313
./120-stamp_dict.t\
1414
./200-dict.t\
1515
./300-galley.t\

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp