|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Copyright 2021 Nikolay Shaplov (Postgres Professional) |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + ******************************************************************************/ |
| 18 | + |
| 19 | +#include<string.h> |
| 20 | + |
| 21 | +#include<exception> |
| 22 | +#include<string> |
| 23 | +#include<cstdlib> |
| 24 | +#include<vector> |
| 25 | +#defineWANT_TEST_EXTRAS |
| 26 | +#include<tap++/tap++.h> |
| 27 | + |
| 28 | +#include"blobstamper/blobstamper.h" |
| 29 | + |
| 30 | +#include"test-chars-stamps.h" |
| 31 | + |
| 32 | +usingnamespaceTAP; |
| 33 | + |
| 34 | +char short_sample[]="1234567"; |
| 35 | +char longer_sample[]="z1234567*89abcde&fghijklmnopqrstuvwxyzAB%CDEFGHIJKLMNOPQRSTUVWXYZ!"; |
| 36 | + |
| 37 | + |
| 38 | +//class SimpeRecursionNode; |
| 39 | +//class TestRNode1; |
| 40 | +//class TestRNode2; |
| 41 | +//class TestRNode3; |
| 42 | + |
| 43 | + |
| 44 | +template<classT>classWeightedRef |
| 45 | +{ |
| 46 | +public: |
| 47 | + std::reference_wrapper<T> ref; |
| 48 | +char weight; |
| 49 | +WeightedRef(T& arg_ref): ref{arg_ref}, weight{1} {} |
| 50 | +WeightedRef(T& arg_ref,char weight_arg): ref{arg_ref}, weight{weight_arg} {} |
| 51 | +}; |
| 52 | + |
| 53 | + |
| 54 | +template<classT>classWeightedRefPicker |
| 55 | +{ |
| 56 | + std::vector<WeightedRef<T>> vec; |
| 57 | +public: |
| 58 | +WeightedRefPicker(std::initializer_list<WeightedRef<T>> arg): vec{arg} {} |
| 59 | + std::reference_wrapper<T>pick(unsignedchar c); |
| 60 | +}; |
| 61 | + |
| 62 | + |
| 63 | +template<classT> std::reference_wrapper<T> |
| 64 | +WeightedRefPicker<T>::pick(unsignedchar c) |
| 65 | +{ |
| 66 | +unsignedchar max =255; |
| 67 | +int total =0; |
| 68 | +for(WeightedRef<T> & wr : vec) |
| 69 | + { |
| 70 | + total += wr.weight; |
| 71 | + } |
| 72 | +double step = (double)max / total; |
| 73 | +int i =0; |
| 74 | +double sum =0; |
| 75 | + |
| 76 | + |
| 77 | +for(WeightedRef<T> & wr : vec) |
| 78 | + { |
| 79 | + sum += step * wr.weight; |
| 80 | +if (c <= sum) |
| 81 | +return wr.ref; |
| 82 | + } |
| 83 | + |
| 84 | +// there mught be problem with precision that will not allow us to catch last item |
| 85 | +// but delta should be small. In other cases terminating |
| 86 | +if (abs(c-sum) >0.1) |
| 87 | + { |
| 88 | +fprintf(stderr,"Something is really wrong\n"); |
| 89 | +exit(1); |
| 90 | + } |
| 91 | +return vec.back().ref; |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +classGalleySimpleRecusion; |
| 98 | + |
| 99 | + |
| 100 | +classSimpeRecursionNode:publicStampBase |
| 101 | +{ |
| 102 | + |
| 103 | +public: |
| 104 | + GalleySimpleRecusion * recursor; |
| 105 | +virtual std::stringdo_recursion(Blob &blob) = 0; |
| 106 | + |
| 107 | +}; |
| 108 | +classTestRNode1:publicSimpeRecursionNode |
| 109 | +{ |
| 110 | +public: |
| 111 | +virtual std::stringdo_recursion(Blob &blob)override; |
| 112 | +intminSize()override {return1; };// FIXME correct it |
| 113 | +intmaxSize()override {return -1;};/* Sereies always takes as much data as it can take*/ |
| 114 | +}; |
| 115 | +classTestRNode2:publicSimpeRecursionNode |
| 116 | +{ |
| 117 | +public: |
| 118 | +virtual std::stringdo_recursion(Blob &blob)override; |
| 119 | +intminSize()override {return1; };// FIXME correct it |
| 120 | +intmaxSize()override {return -1;};/* Sereies always takes as much data as it can take*/ |
| 121 | +}; |
| 122 | +classTestRNode3:publicSimpeRecursionNode |
| 123 | +{ |
| 124 | +public: |
| 125 | +virtual std::stringdo_recursion(Blob &blob)override; |
| 126 | +intminSize()override {return1; };// FIXME correct it |
| 127 | +intmaxSize()override {return -1;};/* Sereies always takes as much data as it can take*/ |
| 128 | +}; |
| 129 | + |
| 130 | +classGalleySimpleRecusion :publicGalleyBase |
| 131 | +{ |
| 132 | + TestRNode1 node1; |
| 133 | + TestRNode2 node2; |
| 134 | + TestRNode3 node3; |
| 135 | + |
| 136 | + std::vector<std::reference_wrapper<SimpeRecursionNode>> stamps = {node1, node2, node3}; |
| 137 | + |
| 138 | +public: |
| 139 | +intminSize()override {return1; };// FIXME correct it |
| 140 | +intmaxSize()override {return -1;};/* Sereies always takes as much data as it can take*/ |
| 141 | + |
| 142 | + |
| 143 | +virtual std::stringdo_recursion(Blob &blob); |
| 144 | + |
| 145 | +}; |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +std::string |
| 151 | +TestRNode1::do_recursion(Blob &blob) |
| 152 | +{ |
| 153 | +try{ |
| 154 | + Blob tmp = blob.ShiftBytes(1); |
| 155 | + } |
| 156 | +catch (OutOfData) |
| 157 | + { |
| 158 | +return"0"; |
| 159 | + } |
| 160 | + |
| 161 | +return"1 +" + recursor->do_recursion(blob); |
| 162 | +} |
| 163 | + |
| 164 | +std::string |
| 165 | +TestRNode2::do_recursion(Blob &blob) |
| 166 | +{ |
| 167 | +try{ |
| 168 | + Blob tmp = blob.ShiftBytes(1); |
| 169 | + } |
| 170 | +catch (OutOfData) |
| 171 | + { |
| 172 | +return"0"; |
| 173 | + } |
| 174 | + |
| 175 | +return"2 +" + recursor->do_recursion(blob); |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | +std::string |
| 180 | +TestRNode3::do_recursion(Blob &blob) |
| 181 | +{ |
| 182 | +try{ |
| 183 | + Blob tmp = blob.ShiftBytes(1); |
| 184 | + } |
| 185 | +catch (OutOfData) |
| 186 | + { |
| 187 | +return"0"; |
| 188 | + } |
| 189 | + |
| 190 | +return"3 +" + recursor->do_recursion(blob); |
| 191 | +} |
| 192 | + |
| 193 | +std::string |
| 194 | +GalleySimpleRecusion::do_recursion(Blob& blob) |
| 195 | +{ |
| 196 | + StampArithm<char> stamp_char; |
| 197 | +char c; |
| 198 | +try{ |
| 199 | + c = stamp_char.ExtractValue(blob); |
| 200 | + } |
| 201 | +catch (OutOfData) |
| 202 | + { |
| 203 | +return"0"; |
| 204 | + } |
| 205 | + |
| 206 | + c = c %3; |
| 207 | + |
| 208 | + SimpeRecursionNode &node = stamps[c]; |
| 209 | +// TestRNode3 node; |
| 210 | + node.recursor =this; |
| 211 | +return node.do_recursion(blob); |
| 212 | +} |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | +int |
| 220 | +main() |
| 221 | + { |
| 222 | + |
| 223 | +char c1='a'; |
| 224 | +char c2='b'; |
| 225 | +char c3='c'; |
| 226 | +char c4='d'; |
| 227 | + |
| 228 | + WeightedRefPicker<char> wrp {{c1,1},{c2,2},{c3,3},{c4,4}}; |
| 229 | + |
| 230 | +for(int i =0; i<=255;i+=1) |
| 231 | + { |
| 232 | +char &rc = wrp.pick(i); |
| 233 | +fprintf(stderr,"^^ %i %c\n",i,rc); |
| 234 | + |
| 235 | + } |
| 236 | + |
| 237 | +TEST_START(1); |
| 238 | + { |
| 239 | + Blobblob(short_sample,strlen(short_sample)); |
| 240 | + GalleySimpleRecusion galley; |
| 241 | + |
| 242 | +fprintf(stderr,"--1\n"); |
| 243 | + |
| 244 | + std::string str = galley.do_recursion(blob); |
| 245 | + |
| 246 | +fprintf(stderr,"--99\n"); |
| 247 | + |
| 248 | +fprintf(stderr,"-- %s --\n",str.c_str()); |
| 249 | + |
| 250 | +ok(1); |
| 251 | + } |
| 252 | + TEST_END; |
| 253 | +} |