computer-science-in-javascript
Version:
Collection of classic computer science paradigms, algorithms, and approaches written in JavaScript.
152 lines (112 loc) • 6.53 kB
HTML
<html>
<head>
<title>Base64 Tests</title>
<!-- Combo-handled YUI CSS files: -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.7.0/build/logger/assets/logger.css&2.7.0/build/yuitest/assets/testlogger.css">
<!-- Combo-handled YUI JS files: -->
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js&2.7.0/build/logger/logger-min.js&2.7.0/build/yuitest/yuitest-min.js"></script>
<script type="text/javascript" src="base64.js"></script>
</head>
<body>
<h1>Base64 Encoding/Decoding Tests</h1>
<script type="text/javascript">
YAHOO.namespace("test");
YAHOO.test.Base64 = (function(){
var assert = YAHOO.util.Assert;
//-------------------------------------------------------------------------
// Base Test Suite
//-------------------------------------------------------------------------
var suite = new YAHOO.tool.TestSuite("Base64 Tests");
//-------------------------------------------------------------------------
// Test Case for encoding
//-------------------------------------------------------------------------
suite.add(new YAHOO.tool.TestCase({
name : "Base 64 Encoding Tests",
_should: {
error: {
testInvalidChar: new Error("Can't base64 encode non-ASCII characters.")
}
},
//---------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------
testMan: function(){
var result = base64Encode("Man");
assert.areEqual("TWFu", result);
},
testHelloWorld: function(){
var result = base64Encode("Hello world");
assert.areEqual("SGVsbG8gd29ybGQ=", result);
},
testPhrase: function(){
var expected = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
var result = base64Encode("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.");
assert.areEqual(expected, result);
},
testInvalidChar: function(){
base64Encode(String.fromCharCode(256) + "Hello!");
}
}));
//-------------------------------------------------------------------------
// Test Case for decoding
//-------------------------------------------------------------------------
suite.add(new YAHOO.tool.TestCase({
name : "Base 64 Decoding Tests",
_should: {
error: {
testInvalidChar: new Error("Not a base64-encoded string."),
testInvalidString: new Error("Not a base64-encoded string."),
testMissingPaddingIndicator: new Error("Not a base64-encoded string.")
}
},
//---------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------
testMan: function(){
var result = base64Decode("TWFu");
assert.areEqual("Man", result);
},
testHelloWorld: function(){
var result = base64Decode("SGVsbG8gd29ybGQ=");
assert.areEqual("Hello world", result);
},
testPhrase: function(){
var result = base64Decode("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=");
var expected = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
assert.areEqual(expected, result);
},
testPhraseWithWhitespace: function(){
var result = base64Decode("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24 sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHB hc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYn kgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aW\ndhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG\t9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=");
var expected = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
assert.areEqual(expected, result);
},
testA: function(){
var expected = "a";
var result = base64Decode("YQ==");
assert.areEqual(expected, result);
},
testMissingPaddingIndicator: function(){
var expected = "hatch";
var result = base64Decode("aGF0Y2g");
assert.areEqual(expected, result);
},
testInvalidChar: function(){
base64Decode("aGF,0Y2g");
},
testInvalidString: function(){
base64Decode("aGF0Y2g==");
}
}));
//return it
return suite;
})();
(function (){
//create the logger
var logger = new YAHOO.tool.TestLogger();
//add the tests
YAHOO.tool.TestRunner.add(YAHOO.test.Base64);
YAHOO.tool.TestRunner.run();
})();
</script>
</body>
</html>