#include "nail_led_mapper.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
static uint8_t weights[1][NLM_GRID_CELLS], probability[NLM_GRID_CELLS];
static void scenario(unsigned numerator,unsigned denominator,unsigned threshold){
 memset(weights,0,sizeof weights);memset(probability,0,sizeof probability);
 unsigned i=0, remaining=numerator;
 while(remaining){unsigned w=remaining>255?255:remaining;weights[0][i]=w;probability[i++]=255;remaining-=w;}
 remaining=denominator-numerator;
 while(remaining){unsigned w=remaining>255?255:remaining;weights[0][i++]=w;remaining-=w;}
 uint8_t max_pwm[1]={200};
 nlm_profile p={1,1,true,weights,max_pwm};
 nlm_policy policy={1,threshold,1,100};
 nlm_frame frame={1,2,0,1,0,true,true,true,255,probability};
 nlm_result result;assert(nlm_map_frame(&p,&policy,&frame,&result)==NLM_OK);
 unsigned expected=numerator*1000u>=denominator*threshold;
 if(result.channel_mask[0]!=expected){fprintf(stderr,"wrong threshold decision: %u/%u threshold=%u mask=%u expected=%u\n",numerator,denominator,threshold,result.channel_mask[0],expected);assert(0);}
 assert(result.pwm[0]==(expected?100:0));
}
int main(void){
 scenario(1999,2000,1000);scenario(1899,2000,950);scenario(1900,2000,950);scenario(1901,2000,950);
 unsigned count=4;
 for(unsigned n=0;n<=2000;n++)for(unsigned t=1;t<=1000;t++){scenario(n,2000,t);count++;}
 printf("PASS %u exact threshold decisions\n",count);
}
