|
| 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 | +#defineWANT_TEST_EXTRAS |
| 25 | +#include<tap++/tap++.h> |
| 26 | + |
| 27 | +#include"blobstamper/blob.h" |
| 28 | +#include"blobstamper/stamp_arithm.h" |
| 29 | +#include"blobstamper/stamp_lottery.h" |
| 30 | + |
| 31 | + |
| 32 | +usingnamespaceTAP; |
| 33 | + |
| 34 | +unsignedchar sample[]={1,63,64,255,1}; |
| 35 | +unsignedchar sample2[]={1,63,64,255,1,63,64,255,1}; |
| 36 | + |
| 37 | +int |
| 38 | +main() |
| 39 | +{ |
| 40 | +TEST_START(10); |
| 41 | + |
| 42 | +/* Tests for common Stamp Lottery*/ |
| 43 | + {/* 1..5*/ |
| 44 | + Blobblob((char *) sample,sizeof(sample)); |
| 45 | + StampArithm<unsignedshort> stamp_s; |
| 46 | + StampArithm<unsignedchar> stamp_c; |
| 47 | + StampArithm<unsignedint> stamp_i; |
| 48 | + StampLottery<StampBaseStr>stamp({stamp_s}); |
| 49 | + |
| 50 | +is(stamp.maxSize(),2+1,"Test Set 1, max size is as expected"); |
| 51 | +is(stamp.minSize(),2+1,"Test Set 1, min size is as expected"); |
| 52 | + |
| 53 | + stamp.Append(stamp_c); |
| 54 | + stamp.Append(stamp_i); |
| 55 | + |
| 56 | +is(stamp.maxSize(),4+1,"Test Set 1.1, max size is as expected"); |
| 57 | +is(stamp.minSize(),1+1,"Test Set 1.1, min size is as expected"); |
| 58 | + |
| 59 | + std::string s = stamp.ExtractStr(blob); |
| 60 | + |
| 61 | +is(s,"16447","Test Set 1, return value is as expected"); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + {/* 6*/ |
| 66 | + sample[0] =255;/* Should choose last stamp*/ |
| 67 | + Blobblob((char *) sample,sizeof(sample)); |
| 68 | + StampArithm<unsignedshort> stamp_s; |
| 69 | + StampArithm<unsignedchar> stamp_c; |
| 70 | + StampArithm<unsignedint> stamp_i; |
| 71 | + StampLottery<StampBaseStr>stamp({stamp_s,stamp_c,stamp_i}); |
| 72 | + |
| 73 | + std::string s = stamp.ExtractStr(blob); |
| 74 | + |
| 75 | +is(s,"33505343","Test Set 2, return value is as expected"); |
| 76 | + } |
| 77 | + |
| 78 | + {/* 7*/ |
| 79 | + sample[0] =128;/* Should choose stamp in the middle*/ |
| 80 | + Blobblob((char *) sample,sizeof(sample)); |
| 81 | + StampArithm<unsignedshort> stamp_s; |
| 82 | + StampArithm<unsignedchar> stamp_c; |
| 83 | + StampArithm<unsignedint> stamp_i; |
| 84 | + StampLottery<StampBaseStr>stamp({stamp_s,stamp_c,stamp_i}); |
| 85 | + |
| 86 | + std::string s = stamp.ExtractStr(blob); |
| 87 | + |
| 88 | +is(s,"63","Test Set 3, return value is as expected"); |
| 89 | + } |
| 90 | + |
| 91 | + {/* 8*/ |
| 92 | + sample[0] =1;/* Should choose first available stamp*/ |
| 93 | + Blobblob((char *) sample,2);/* Sic!*/ |
| 94 | + StampArithm<unsignedshort> stamp_s; |
| 95 | + StampArithm<unsignedchar> stamp_c; |
| 96 | + StampArithm<unsignedlong> stamp_l; |
| 97 | + StampLottery<StampBaseStr>stamp({stamp_s,stamp_l,stamp_c}); |
| 98 | + |
| 99 | + std::string s = stamp.ExtractStr(blob); |
| 100 | +/* Only char stamp will be chosen as only it has enough data*/ |
| 101 | +is(s,"63","Test Set 4, return value is as expected"); |
| 102 | + } |
| 103 | + |
| 104 | +/* Tests for common Stamp Lottery that is used for recursion*/ |
| 105 | + |
| 106 | + |
| 107 | + {/* 9*/ |
| 108 | + sample2[0] =1;/* Should choose first available stamp*/ |
| 109 | + Blobblob((char *) sample2,sizeof(sample2)); |
| 110 | + StampArithm<unsignedshort> stamp_s; |
| 111 | + StampArithm<unsignedchar> stamp_c; |
| 112 | + StampArithm<unsignedlong> stamp_l; |
| 113 | + StampLottery4Recursion<StampBaseStr>stamp({stamp_s,stamp_c,stamp_l}); |
| 114 | + |
| 115 | + std::string s = stamp.ExtractStr(blob); |
| 116 | +/* Only unsigned long stamp will be chosen as other stamps does not consume enough data*/ |
| 117 | +is(s,"143904352459767871","Test Set 5, return value is as expected"); |
| 118 | + } |
| 119 | + |
| 120 | + {/* 10*/ |
| 121 | + sample2[0] =255;/* Should choose last available stamp*/ |
| 122 | + Blobblob((char *) sample2,sizeof(sample2) -1 );/*One byte short for unsigned long*/ |
| 123 | + StampArithm<unsignedshort> stamp_s; |
| 124 | + StampArithm<unsignedchar> stamp_c; |
| 125 | + StampArithm<unsignedlong> stamp_l; |
| 126 | + StampLottery4Recursion<StampBaseStr>stamp({stamp_s,stamp_c,stamp_l}); |
| 127 | + |
| 128 | + std::string s = stamp.ExtractStr(blob); |
| 129 | +/* l-stamp is to long. s & c stamps are too short. When no stamps left, it will ignore "too short" limit, and will chose last available stamp: c*/ |
| 130 | +is(s,"63","Test Set 6, return value is as expected"); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + TEST_END; |
| 136 | +} |