50 lines
2.8 KiB
C
50 lines
2.8 KiB
C
#include <assert.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
static uint32_t comparisons;
|
|
static int counted_less(uint32_t a,uint32_t b){++comparisons;return a<b;}
|
|
#define GW_HOTFIX_SPLIT_LESS(a,b) counted_less(a,b)
|
|
#include "hotfix_request_split.h"
|
|
struct observation {unsigned calls,fail_at;uint32_t ids[4096],data_build;};
|
|
static void put(unsigned char* p,uint32_t value){for(unsigned i=0;i<4;++i)p[i]=(unsigned char)(value>>(i*8));}
|
|
static int collect(void* context,const unsigned char* request,uint32_t size){
|
|
struct observation* o=context;assert(size==12||size==16);
|
|
assert(gw_hotfix_u32(request)==70334);assert(gw_hotfix_u32(request+4)==o->data_build);
|
|
assert(gw_hotfix_u32(request+8)==(size==16));
|
|
o->ids[o->calls++]=size==16?gw_hotfix_u32(request+12):0;
|
|
return o->fail_at==o->calls?37:0;
|
|
}
|
|
int main(void){
|
|
unsigned char body[12+4096*4]={0};struct observation o={0};
|
|
put(body,70334);put(body+4,65617);o.data_build=65617;
|
|
assert(!gw_hotfix_split_request(body,12,collect,&o)&&o.calls==1&&o.ids[0]==0);
|
|
put(body+8,4);put(body+12,111);put(body+16,222);put(body+20,111);put(body+24,333);o.calls=0;
|
|
assert(!gw_hotfix_split_request(body,28,collect,&o)&&o.calls==3);
|
|
assert(o.ids[0]==111&&o.ids[1]==222&&o.ids[2]==333);
|
|
o.calls=0;o.fail_at=2;
|
|
assert(gw_hotfix_split_request(body,28,collect,&o)==37&&o.calls==2);
|
|
o.calls=0;o.fail_at=0;
|
|
for(unsigned length=0;length<28;++length)assert(gw_hotfix_split_request(body,length,collect,&o)==-1);
|
|
assert(gw_hotfix_split_request(body,29,collect,&o)==-1&&!o.calls);
|
|
put(body,0);assert(gw_hotfix_split_request(body,28,collect,&o)==-1&&!o.calls);put(body,70334);
|
|
put(body+8,4097);assert(gw_hotfix_split_request(body,sizeof(body),collect,&o)==-1&&!o.calls);
|
|
put(body+8,4096);for(unsigned i=0;i<4096;++i)put(body+12+4*i,i+1);
|
|
comparisons=0;
|
|
assert(!gw_hotfix_split_request(body,sizeof(body),collect,&o)&&o.calls==4096);
|
|
assert(comparisons<200000);printf("4096 ascending IDs: %u comparisons (previously 8386560)\n",comparisons);
|
|
assert(o.ids[0]==1&&o.ids[4095]==4096);
|
|
/* Worst ordering, duplicate-heavy input and the full unsigned ID range. */
|
|
for(unsigned mode=0;mode<3;++mode){
|
|
uint32_t expected=mode==1?17:4096;unsigned char before[sizeof(body)];
|
|
for(unsigned i=0;i<4096;++i)put(body+12+4*i,mode==0?0xffffffffu-i:mode==1?i%17:i*2654435761u);
|
|
memcpy(before,body,sizeof(body));o.calls=0;comparisons=0;
|
|
assert(!gw_hotfix_split_request(body,sizeof(body),collect,&o)&&o.calls==expected);
|
|
assert(comparisons<200000&&!memcmp(before,body,sizeof(body)));
|
|
for(unsigned i=0;i<expected;++i)assert(o.ids[i]==(mode==0?0xffffffffu-i:mode==1?i:i*2654435761u));
|
|
}
|
|
assert(gw_hotfix_split_request(0,12,collect,&o)==-1);
|
|
assert(gw_hotfix_split_request(body,12,0,&o)==-1);
|
|
return 0;
|
|
}
|