fraction_Test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Created by Oliver on 16.03.2016.
  3. */
  4. import { Fraction } from "../../../src/Common/DataObjects/fraction";
  5. import * as Collections from "../../../node_modules/typescript-collections";
  6. describe("Fraction Unit Tests:", () => {
  7. describe("Construct Fraction, check properties", () => {
  8. let f1: Fraction = new Fraction(2, 6);
  9. it("Numerator and Denominator", (done: MochaDone) => {
  10. chai.expect(f1.Numerator).to.equal(1);
  11. chai.expect(f1.Denominator).to.equal(3);
  12. done();
  13. });
  14. it("Real value", (done: MochaDone) => {
  15. chai.expect(f1.RealValue).to.equal(1 / 3);
  16. done();
  17. });
  18. });
  19. describe("Compare fractions", () => {
  20. let f1: Fraction;
  21. let f2: Fraction;
  22. let rand: () => number = function(): number {
  23. return Math.floor(Math.random() * 500) + 1;
  24. };
  25. it("lt attribute", (done: MochaDone) => {
  26. for (let i: number = 0; i < 10; i += 1) {
  27. f1 = new Fraction(rand(), rand());
  28. f2 = new Fraction(rand(), rand());
  29. chai.expect(f1.lt(f2)).to.equal(f1.RealValue < f2.RealValue);
  30. }
  31. done();
  32. });
  33. });
  34. // Todo: remove when typescript porting phase 2 is done an project is compiling properly again
  35. describe("blablabla", () => {
  36. let dict: Collections.Dictionary<Fraction, Fraction> = new Collections.Dictionary<Fraction, Fraction>();
  37. // new Collections.Dictionary<Fraction, Fraction>(
  38. // function(f: Fraction): string {
  39. // return f.toString();
  40. // });
  41. let keys: Fraction[] = [];
  42. let values: Fraction[] = [];
  43. for (let i: number = 0; i < 10; ++i) {
  44. keys.push(new Fraction(1, i));
  45. values.push(new Fraction(i, 1));
  46. dict.setValue(keys[i], values[i]);
  47. }
  48. it("retrieved fractions should be equal", (done: MochaDone) => {
  49. for (let i: number = 9; i > -1; --i) {
  50. let key: Fraction = keys[i];
  51. let value: Fraction = values[i];
  52. //console.log(values[i].toString() + "== " + dict.getValue(key));
  53. console.log(values[i].toString() + "== " + dict.getValue(new Fraction(key.Numerator, key.Denominator)));
  54. // chai.expect(dict.getValue(key)).to.equal(value);
  55. chai.expect(dict.getValue(new Fraction(key.Numerator, key.Denominator))).to.equal(value);
  56. }
  57. done();
  58. });
  59. });
  60. });