util.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*jshint expr:true */
  2. var expect = require('chai').expect,
  3. util = require('../src/server/lib/util');
  4. /**
  5. * Tests for server/lib/util.js
  6. *
  7. * This is mostly a regression suite, to make sure behavior
  8. * is preserved throughout changes to the server infrastructure.
  9. */
  10. describe('util.js', function () {
  11. describe('#massToRadius', function () {
  12. it('should return non-zero radius on zero input', function () {
  13. var r = util.massToRadius(0);
  14. expect(r).to.be.a('number');
  15. expect(r).to.equal(4);
  16. });
  17. it('should convert masses to a circle radius', function () {
  18. var r1 = util.massToRadius(4),
  19. r2 = util.massToRadius(16),
  20. r3 = util.massToRadius(1);
  21. expect(r1).to.equal(16);
  22. expect(r2).to.equal(28);
  23. expect(r3).to.equal(10);
  24. });
  25. });
  26. describe('#validNick', function () {
  27. it('should allow empty player nicknames', function () {
  28. var bool = util.validNick('');
  29. //expect(bool).to.be.true;
  30. });
  31. it('should allow ascii character nicknames', function () {
  32. var n1 = util.validNick('Walter_White'),
  33. n2 = util.validNick('Jesse_Pinkman'),
  34. n3 = util.validNick('hank'),
  35. n4 = util.validNick('marie_schrader12'),
  36. n5 = util.validNick('p');
  37. expect(n1).to.be.true;
  38. expect(n2).to.be.true;
  39. expect(n3).to.be.true;
  40. expect(n4).to.be.true;
  41. expect(n5).to.be.true;
  42. });
  43. it('should disallow unicode-dependent alphabets', function () {
  44. var n1 = util.validNick('Йèæü');
  45. expect(n1).to.be.false;
  46. });
  47. it('should disallow spaces in nicknames', function () {
  48. var n1 = util.validNick('Walter White');
  49. expect(n1).to.be.false;
  50. });
  51. });
  52. describe('#log', function () {
  53. it('should compute the log_{base} of a number', function () {
  54. var base10 = util.log(1, 10),
  55. base2 = util.log(1, 2);
  56. var identity = util.log(10, 10);
  57. var logNineThree = Math.round(util.log(9,3) * 1e5) / 1e5; // Tolerate rounding errors
  58. // log(1) should equal 0, no matter the base
  59. expect(base10).to.eql(base2);
  60. // log(n,n) === 1
  61. expect(identity).to.eql(1);
  62. // perform a trivial log calculation: 3^2 === 9
  63. expect(logNineThree).to.eql(2);
  64. });
  65. });
  66. describe('#getDistance', function () {
  67. // helper class
  68. function Point(x,y,r) {
  69. return {
  70. x : x,
  71. y : y,
  72. radius : r
  73. };
  74. }
  75. var p1 = Point(-100, 20, 1),
  76. p2 = Point(0, 40, 5),
  77. p3 = Point(0,0,100);
  78. it('should return a positive number', function () {
  79. var d = util.getDistance(p1, p2);
  80. expect(d).to.be.above(-1);
  81. });
  82. });
  83. });