Home /Modules/advantage/TaUse18RefuseLongAndBadInput.sleela

TaUse18RefuseLongAndBadInput.sleela

lib/advantage/trading-actions-on-verb/18-refuse-long-and-bad-input/TaUse18RefuseLongAndBadInput.sleela · 137 line(s) · sleela

<< lib/advantage/trading-actions-on-verb/18-refuse-long-and-bad-input/TaUse18RefuseLongAndBadInput.sleela
   SLeeLa trading-action use case.
   Definition: Trading short, refusals: a cover above the shares short, bad inputs, a rate change while short and covers from flat are refused and the book never changes. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Use case 18 (style, short). A short-only book never goes long. Every refusal >
< must leave the book exactly as it was; five values are compared separately. >
< The borrow rate may not change while any share is short. >
class TaUse18RefuseLongAndBadInput {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs. A worker-class
     lesson carries the baseline identity record. >>
  String metaVersion = "1.0.0";
  int metaId = 39318;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-11-11";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-11-11";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 5;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }


  int passed = 0;
  int failed = 0;

  void expect(String label, int got, int want) {
    if (got == want) {
      print("  ok   " + label + " = " + got);
      passed = passed + 1;
    } else {
      print("  FAIL " + label + " got " + got + " want " + want);
      failed = failed + 1;
    }
  }

  void expectTrue(String label, boolean got) {
    if (got) {
      print("  ok   " + label);
      passed = passed + 1;
    } else {
      print("  FAIL " + label);
      failed = failed + 1;
    }
  }

  void summary() {
    print("  checks passed=" + passed + " failed=" + failed);
  }

  void act(SLLongBook b, String text, int code) {
    print("  " + text + " : " + b.reason(code));
  }

  int sQty = 0;
  int sProceeds = 0;
  int sFees = 0;
  int sDay = 0;
  int sBorrow = 0;

  void take(SLShortBook b) {
    sQty = b.totalShort();
    sProceeds = b.proceedsHeld();
    sFees = b.fees();
    sDay = b.currentDay();
    sBorrow = b.borrowCostPaid();
  }

  boolean same(SLShortBook b) {
    return sQty == b.totalShort() && sProceeds == b.proceedsHeld() && sFees == b.fees() && sDay == b.currentDay() && sBorrow == b.borrowCostPaid();
  }

  void act2(SLShortBook b, String text, int code) {
    print("  " + text + " : " + b.reason(code));
  }

  void main() {
    SLShortBook book = new SLShortBook();
    print("== Use case 18: refuse long and bad input ==");
    expect("set the borrow rate", book.setBorrowRate(3), 0);
    act2(book, "short 100 @ 5000 on day 10, fee 100", book.shortSell(10, 100, 5000, 100));
    take(book);
    act2(book, "cover 101 @ 4900 (more than short)", book.cover(11, 101, 4900, 100));
    expect("code for cover above the shares short", book.cover(11, 101, 4900, 100), 3);
    expect("code for zero quantity", book.cover(11, 0, 4900, 100), 1);
    expect("code for zero price", book.cover(11, 10, 0, 100), 2);
    expect("code for negative fee", book.cover(11, 10, 4900, 0 - 1), 6);
    expect("code for day going backwards", book.cover(9, 10, 4900, 100), 5);
    expect("code for a price above the size bound", book.cover(11, 10, 2000000000, 0), 8);
    expect("code for a day above the day bound", book.shortSell(20000000, 1, 5100, 0), 8);
    expect("code for changing the rate while short", book.setBorrowRate(4), 9);
    expect("code for a rate above the bound", book.setBorrowRate(5000), 8);
    expectTrue("book unchanged after every refusal", same(book));
    expect("borrow rate unchanged", book.borrowRate, 3);
    expectTrue("invariants hold", book.invariantsHold());
    print("-- an exact cover is fine, then flat means no more covers --");
    act2(book, "cover 100 @ 4900 on day 12, fee 100", book.cover(12, 100, 4900, 100));
    expect("shares short", book.totalShort(), 0);
    expect("code for covering from flat", book.cover(13, 1, 4900, 100), 3);
    expect("code for coverAll from flat", book.coverAll(13, 4900, 100), 7);
    expect("the rate can change again when flat", book.setBorrowRate(4), 0);
    expectTrue("invariants hold", book.invariantsHold());
    summary();
  }
}