Skip to content

Instantly share code, notes, and snippets.

@Madsy
Created March 3, 2018 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Madsy/7c3977804b8d68cd154db299c0198f94 to your computer and use it in GitHub Desktop.
Save Madsy/7c3977804b8d68cd154db299c0198f94 to your computer and use it in GitHub Desktop.
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define NUM_TESTS_RUNS 10
#define MAX_S_ELEMS 16
#define MAX_S_MEMBER_VAL 31
typedef unsigned int u32;
u32 rng(int bias, int range){
double norm = ((double)rand() / ((double)RAND_MAX));
return (double)bias + (double)range*norm;
}
u32 S_to_bit_vector(u32* S, int count){
//assert(count <= 32);
u32 out = 0;
for(int i = 0; i < count; i++){
out |= 1<<S[i];
}
return out;
}
inline u32 f_S(u32 S_bv, u32 s){
return S_bv & (1<<s);
}
void create_random_S_set(u32* S, u32 capacity, u32* size_out){
u32 len = rng(1, MAX_S_ELEMS);
for(int j = 0; j < len; j++){
S[j] = rng(0, MAX_S_MEMBER_VAL);
}
*size_out = len;
}
void print_S(u32* S, u32 size){
printf("S: {%u", S[0]);
for(int i = 1; i < size; i++){
printf(", %u", S[i]);
}
printf("}\n");
}
int main()
{
u32 S[MAX_S_ELEMS];
#define S_CAPACITY (sizeof(S) / sizeof(u32))
u32 S_size = 0;
for(int i = 0; i < NUM_TESTS_RUNS; i++){
printf("==== Running test %d out of %d: ====\n", i, NUM_TESTS_RUNS);
create_random_S_set(S, S_CAPACITY, &S_size);
print_S(S, S_size);
u32 S_bv = S_to_bit_vector(S, S_size);
for(int j = 0; j < 10; j++){
u32 s = rng(0, MAX_S_MEMBER_VAL);
u32 test = f_S(S_bv, s);
printf("Is s(%u) a member of S?: %s\n", s, (test ? "Yes" : "No"));
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment