Fraction_Test.ts 2.5 KB

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