static int base64(void) { unsigned char bits[256]; size_t n; #define B64B(x, l, y) CALL(b64_check((unsigned char *)x, l, y)) #define B64(x, y) B64B(x, strlen(x), y) /* invent these with * $ printf "string" | uuencode -m blah */ B64("a", "YQ=="); B64("bb", "YmI="); B64("ccc", "Y2Nj"); B64("Hello, world", "SGVsbG8sIHdvcmxk"); B64("Aladdin:open sesame", "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="); B64("I once saw a dog called norman.\n", "SSBvbmNlIHNhdyBhIGRvZyBjYWxsZWQgbm9ybWFuLgo="); B64("The quick brown fox jumped over the lazy dog", "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2c="); /* binary data.. * $ printf "string" | wc -c # get the length * $ printf "string" | uuencode -m blah # get the base64 */ B64B("\0\0\0\0\0\n", 6, "AAAAAAAK"); B64B("I once wished \0 upon a \0 fish.", 30, "SSBvbmNlIHdpc2hlZCAAIHVwb24gYSAAIGZpc2gu"); B64B("\201\202\203\204", 4, "gYKDhA=="); for (n = 0; n < sizeof bits; n++) bits[n] = (unsigned char)n; CALL(b64_check(bits, sizeof bits, ALLBITS)); #undef B64 #undef B64B return OK; }
int main(int argc, char **argv) { /* Base64 test vectors from RFC 4648, with padding removed */ /* BASE64("") = "" */ EXPECT_SLICE_EQ("", B64("")); /* BASE64("f") = "Zg" */ EXPECT_SLICE_EQ("Zg", B64("f")); /* BASE64("fo") = "Zm8" */ EXPECT_SLICE_EQ("Zm8", B64("fo")); /* BASE64("foo") = "Zm9v" */ EXPECT_SLICE_EQ("Zm9v", B64("foo")); /* BASE64("foob") = "Zm9vYg" */ EXPECT_SLICE_EQ("Zm9vYg", B64("foob")); /* BASE64("fooba") = "Zm9vYmE" */ EXPECT_SLICE_EQ("Zm9vYmE", B64("fooba")); /* BASE64("foobar") = "Zm9vYmFy" */ EXPECT_SLICE_EQ("Zm9vYmFy", B64("foobar")); EXPECT_SLICE_EQ("wMHCw8TF", B64("\xc0\xc1\xc2\xc3\xc4\xc5")); /* Huffman encoding tests */ EXPECT_SLICE_EQ("\xf1\xe3\xc2\xe5\xf2\x3a\x6b\xa0\xab\x90\xf4\xff", HUFF("www.example.com")); EXPECT_SLICE_EQ("\xa8\xeb\x10\x64\x9c\xbf", HUFF("no-cache")); EXPECT_SLICE_EQ("\x25\xa8\x49\xe9\x5b\xa9\x7d\x7f", HUFF("custom-key")); EXPECT_SLICE_EQ("\x25\xa8\x49\xe9\x5b\xb8\xe8\xb4\xbf", HUFF("custom-value")); EXPECT_SLICE_EQ("\xae\xc3\x77\x1a\x4b", HUFF("private")); EXPECT_SLICE_EQ( "\xd0\x7a\xbe\x94\x10\x54\xd4\x44\xa8\x20\x05\x95\x04\x0b\x81\x66\xe0\x82" "\xa6\x2d\x1b\xff", HUFF("Mon, 21 Oct 2013 20:13:21 GMT")); EXPECT_SLICE_EQ( "\x9d\x29\xad\x17\x18\x63\xc7\x8f\x0b\x97\xc8\xe9\xae\x82\xae\x43\xd3", HUFF("https://www.example.com")); /* Various test vectors for combined encoding */ EXPECT_COMBINED_EQUIV(""); EXPECT_COMBINED_EQUIV("f"); EXPECT_COMBINED_EQUIV("fo"); EXPECT_COMBINED_EQUIV("foo"); EXPECT_COMBINED_EQUIV("foob"); EXPECT_COMBINED_EQUIV("fooba"); EXPECT_COMBINED_EQUIV("foobar"); EXPECT_COMBINED_EQUIV("www.example.com"); EXPECT_COMBINED_EQUIV("no-cache"); EXPECT_COMBINED_EQUIV("custom-key"); EXPECT_COMBINED_EQUIV("custom-value"); EXPECT_COMBINED_EQUIV("private"); EXPECT_COMBINED_EQUIV("Mon, 21 Oct 2013 20:13:21 GMT"); EXPECT_COMBINED_EQUIV("https://www.example.com"); EXPECT_COMBINED_EQUIV( "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"); expect_binary_header("foo-bin", 1); expect_binary_header("foo-bar", 0); expect_binary_header("-bin", 0); return all_ok ? 0 : 1; }
int main(int argc, char *argv[]){ int truncOff; // How far from end to truncate int nread; // Where to truncate from truncOff = 0; if ( argc > 1 ) { nread=sscanf(argv[1],"%d",&truncOff); if ( nread != 1 ) { fprintf(stderr,"Usage: ./a.out truncOff\n"); exit(-1); } } // Variables // Problem size int Nl=100; int Nt=100; int NtCycl=10; // Assumed to be integer when checking reference // Index variables initial values int jNM1orig; int jNM1=0; int jN=1; int jNP1=2; // long unsigned const number = B64(00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000001, 00000101); printf("number %lu\n",number); // Loop counters int i, j; // Scalars double_cast dt2=(double_cast)pow((double)1.0/(double)Nt,(double)2); double_cast dx2=(double_cast)pow((double)1.0/(double)Nl,(double)2); double_cast c2=(double_cast)((double)1.0); // Time steppig uVals array double_cast uVals[3][Nl]; j=0; double vTmp; for (i=0;i<Nl;++i) { vTmp=sin(2.*M_PI/((double)Nl)*(double)((double)i+0.5))*cos(2.*M_PI/(double)Nt*(double)(j)); #ifdef TRUNC_ON vTmp=bitTrunc(vTmp,truncOff); #endif uVals[jNM1][i]=(double_cast)(vTmp); } j=1; for (i=0;i<Nl;++i) { vTmp=sin(2.*M_PI/((double)Nl)*(double)((double)i+0.5))*cos(2.*M_PI/(double)Nt*(double)(j)); #ifdef TRUNC_ON vTmp=bitTrunc(vTmp,truncOff); #endif uVals[jN][i]=(double_cast)(vTmp); } printf("%le\n",M_PI); for (i=0;i<Nl;++i) { printf("%le\n",uVals[jN][i].f); } // Calculate reference solution (at end time) // time runs fron 0 to dt*(Nt-1) double uRef[Nl]; j=Nt-1; for (i=0;i<Nl;++i) { vTmp=sin(2.*M_PI/((double)Nl)*(double)((double)i+0.5))*cos(2.*M_PI/(double)Nt*(double)(j)); // vTmp=bitTrunc(vTmp,truncOff); uRef[i]=(double)(vTmp); } // Calculate numerical solution double_cast v1, v2; int iM1, iP1; for (j=2;j<Nt*NtCycl;++j) { for (i=0;i<Nl;++i) { iM1=i-1; if ( iM1 < 0 ) { iM1 = Nl-1; } iP1=i+1; if ( iP1 == Nl ) { iP1 = 0; } v1=(double_cast)(2.*uVals[jN][i].f-uVals[jNM1][i].f); v2=(double_cast)( dt2.f*c2.f*( uVals[jN][iP1].f + uVals[jN][iM1].f - 2.*uVals[jN][i].f )/dx2.f ); vTmp=v1.f+v2.f; #ifdef TRUNC_ON vTmp=bitTrunc(vTmp,truncOff); #endif uVals[jNP1][i].f=vTmp; } jNM1orig=jNM1; jNM1=jN; jN=jNP1; jNP1=jNM1orig; } //calculate l1,l2 and infinity norm (Jenny) double diff[Nl]; double l1, l2, infinity_norm; for (i=0;i<Nl;++i) { printf("%le %le %le\n",uVals[jN][i].f,uRef[i],uRef[i]-uVals[jN][i].f); diff[i] = uRef[i]-uVals[jN][i].f; } l1 = 0.0; l2 = 0.0; infinity_norm = diff[0]; for (i=0;i<Nl;i++){ l1 = l1+fabs(diff[i]); l2 = l2+pow(diff[i], 2); if (fabs(diff[i])>infinity_norm){ infinity_norm = fabs(diff[i]); } } l2 = pow(l2, 0.5); printf("l1 =%le l2 =%le infinity_norm =%le\n", l1, l2, infinity_norm); }