/* SPDX-License-Identifier: GPL-3.0-or-later */ #include #include #include #include "rules.h" #include "reference_cases.h" static QOptions defaults = {0,Q_MODERN,0,1}; static QGame game; static void pieces(void) { unsigned t,r,x,y,n; int minx,maxx,miny; QPiece p; QBoard empty = {{{0}}}; for (t=0;t<7;++t) for (r=0;r<4;++r) { assert(q_masks[t][r]==reference_masks[t][r]); n=0;minx=miny=4;maxx=-1; for (y=0;y<4;++y) for (x=0;x<4;++x) if (q_occupied(t,r,x,y)) { ++n; if ((int)xmaxx) maxx=x; if ((int)ydifficulty;game.options.scoring=c->rule; game.lines=c->prior;game.score=0; q_score_clear(&game,c->rows,c->bottom); assert(game.level==c->level && game.score==c->points); } game.options.difficulty=0; game.score=Q_SCORE_MAX-5;game.lines=Q_SCORE_MAX-1; q_score_clear(&game,4,1); assert(game.lines==Q_SCORE_MAX && game.score==Q_SCORE_MAX && game.level==65535); for(i=1;i<=100;++i) assert(q_gravity_ms(i)==reference_gravity[i-1]); } /* Adapted from Brian Holman's GPL-3.0-or-later Mac test-scoring.vala: * drop an actual vertical I piece through one-cell gaps in nearly full rows. * This also checks bottom-row detection and level changes, not just arithmetic. */ static void scoring_through_drop(void) { unsigned i,x;int y,bottom; for(i=0;idifficulty;game.options.scoring=c->rule; game.lines=c->prior;game.level=q_level(c->difficulty,c->prior); bottom=c->bottom?19:18; for(y=bottom-(int)c->rows+1;y<=bottom;++y) for(x=0;x<10;++x)if(x!=4)game.board.cells[y][x]=1; if(!c->bottom)game.board.cells[19][4]=1; game.active=q_spawn(5,1);game.active.x=3;game.active.y=0; assert(q_drop(&game)); assert(game.last_clear==c->rows); if(c->rows)assert(game.last_bottom==c->bottom); assert(game.lines==c->prior+c->rows); assert(game.level==c->level&&game.score==c->points); } } static void hold(void) { QChoice preview; Q8 original,held; QRandom random; q_start(&game,&defaults); original=game.active.type;preview=game.next; assert(q_hold(&game)); assert(game.held==original && game.active.type==preview.type && game.active.rotation==0); random=game.random;preview=game.next; assert(!q_hold(&game));assert(memcmp(&random,&game.random,sizeof(random))==0); assert(q_drop(&game));assert(!game.hold_used); held=game.held;original=game.active.type;preview=game.next;random=game.random; assert(q_hold(&game)); assert(game.active.type==held && game.active.rotation==0 && game.held==original); assert(memcmp(&preview,&game.next,sizeof(preview))==0); assert(memcmp(&random,&game.random,sizeof(random))==0); assert(!q_hold(&game)); } static void randomness(void) { QRandom a,b; QOptions options=defaults; QGame other; unsigned i,j,x,y;unsigned selected[7]={0};Q16 lo,hi; q_random_init(&a,1); assert(q_random_next(&a)==270369UL); assert(q_random_next(&a)==67634689UL); assert(q_random_next(&a)==2647435461UL); q_random_init(&a,0);q_random_init(&b,0); for(i=0;i<100;++i)assert(q_weighted(&a)==q_weighted(&b)); q_random_init(&a,1234567); for(i=0;i<20000;++i)++selected[q_weighted(&a)]; lo=hi=selected[0]; for(i=0;i<7;++i){if(selected[i]hi)hi=selected[i];} assert(hi-lo<20); /* Fixed count vector: underrepresented types must be much likelier. */ memset(selected,0,sizeof(selected));q_random_init(&a,0x12345678UL); for(i=0;i<20000;++i){for(j=0;j<7;++j)a.counts[j]=0;a.counts[0]=10;++selected[q_weighted(&a)];} assert(selected[0]>0 && selected[0]<100); for(i=1;i<7;++i)assert(selected[i]>2500); for(i=0;i<32;++i) for(j=0;j<=16;++j) { options.seed=i;options.difficulty=(int)j-1; q_start(&game,&options);q_start(&other,&options); assert(memcmp(&game.board,&other.board,sizeof(game.board))==0); assert(game.active.type==other.active.type && game.next.type==other.next.type); for(y=0;y<20;++y){unsigned count=0;for(x=0;x<10;++x){assert(game.board.cells[y][x]<=7);count+=game.board.cells[y][x]!=0;}assert(count<10);} } } static void difficult(void) { static const int empty_metric[7]={-20,-20,-20,-25,-25,-20,-20}; QBoard b={{{0}}},before; QRandom a,c; QChoice choice,other; unsigned i; before=b; for(i=0;i<7;++i)assert(q_best_metric(&b,i)==empty_metric[i]); assert(memcmp(&b,&before,sizeof(b))==0); for(i=0;i<10;++i)if(i<3||i>6)b.cells[19][i]=1; before=b; assert(q_best_metric(&b,5)==5000); assert(memcmp(&b,&before,sizeof(b))==0); q_random_init(&a,47);q_random_init(&c,47); choice=q_difficult(&b,&a);other=q_difficult(&b,&c); assert(choice.type<7 && choice.rotation<4); assert(choice.type==other.type && choice.rotation==other.rotation); assert(memcmp(&b,&before,sizeof(b))==0); memset(&b,1,sizeof(b)); for(i=0;i<7;++i)assert(q_best_metric(&b,i)==-30000L); } int main(void) { pieces();rotation_kicks();movement_and_lock();simultaneous_rows(); scoring();scoring_through_drop();hold();randomness();difficult(); printf("PASS: 28 states, all kicks, bounds/top-out, compaction, %u score fixtures plus actual-drop scoring, gravity, hold, RNG/prefill, difficult metrics\n",(unsigned)(sizeof(score_cases)/sizeof(score_cases[0]))); return 0; }