Skip to content
Permalink
Browse files

Detailed exception messages

  • Loading branch information
samchon committed Jun 14, 2019
1 parent bb655f4 commit 816b9ea6b459b2c725954fdc9cd359347f820b2a
@@ -1,6 +1,7 @@
# ======================================================
# CUSTOM IGNORE
# ======================================================
assets/benchmarks/
node_modules/
lib/

@@ -5,9 +5,11 @@
.github/
.vscode/

assets/
node_modules/
build/
src/
benchmark/
test/

*.log
@@ -1,4 +1,6 @@
# **T**ypeScript **S**tandard **T**emplate **L**ibrary
![Make STL Great Again](assets/images/logo.png)
[![Build Status](https://travis-ci.org/samchon/tstl.svg?branch=master)](https://travis-ci.org/samchon/tstl)
[![npm version](https://badge.fury.io/js/tstl.svg)](https://www.npmjs.com/package/tstl)
[![Downloads](https://img.shields.io/npm/dm/tstl.svg)](https://www.npmjs.com/package/tstl)
Binary file not shown.
@@ -6,11 +6,12 @@
"email": "samchon@samchon.org",
"url": "http://samchon.org"
},
"version": "2.2.0-dev.20190614",
"version": "2.2.0-dev.20190615",
"main": "./index.js",
"typings": "./index.d.ts",
"scripts": {
"build": "tsc && npm run test",
"benchmark": "node benchmark",
"test": "node test",
"api": "node build/api",
"clean": "node build/clean"
@@ -10,7 +10,7 @@ import { ArrayIteratorBase, ArrayReverseIteratorBase } from "../iterator/ArrayIt

import { _Repeater } from "../iterator/_Repeater";
import { RangeError } from "../../exception/RuntimeError";
import { InvalidArgument, LengthError } from "../../exception/LogicError";
import { InvalidArgument, OutOfRange } from "../../exception/LogicError";

/**
* Base array container.
@@ -139,9 +139,9 @@ export abstract class ArrayContainer<T extends ElemT,
{
// VALIDATION
if (pos._Get_array() !== <any>this)
throw new InvalidArgument("Parametric iterator is not this container's own.");
throw new InvalidArgument(`Error on std.${this.constructor.name}.insert(): parametric iterator is not this container's own.`);
else if (pos.index() < 0)
throw new LengthError("Parametric iterator is directing invalid position.");
throw new OutOfRange(`Error on std.${this.constructor.name}.insert(): parametric iterator is directing negative position -> (index = ${pos.index()}).`);
else if (pos.index() > this.size())
pos = this.end();

@@ -191,15 +191,15 @@ export abstract class ArrayContainer<T extends ElemT,
{
// VALIDATION
if (first._Get_array() !== <any>this || last._Get_array() !== <any>this)
throw new InvalidArgument("Parametric iterator is not this container's own.");
throw new InvalidArgument(`Error on std.${this.constructor.name}.erase(): parametric iterator is not this container's own.`);
else if (first.index() < 0)
throw new LengthError("Invalid parameter: first is directing negative index.");
throw new OutOfRange(`Error on std.${this.constructor.name}.erase(): first is directing negative position -> (first = ${first.index()}).`);
else if (first.index() > last.index())
throw new RangeError(`Error on std.${this.constructor.name}.erase(): first iterator has greater index than last -> (first = ${first.index()}, last = ${last.index()}).`);

// ADJUSTMENTS
// ADJUSTMENT
if (first.index() >= this.size())
return this.end();
else if (first.index() > last.index())
throw new RangeError("Invalid range. Paramter first is greater than last.");

// ERASE ELEMENTS
return this._Erase_by_range(first, last);
@@ -210,7 +210,7 @@ export abstract class ListContainer<T,
{
// VALIDATION
if (pos.source() !== this.end_.source())
throw new InvalidArgument("Parametric iterator is not this container's own.");
throw new InvalidArgument(`Error on std.${this.end_.source().constructor.name}.insert(): parametric iterator is not this container's own.`);

// BRANCHES
if (args.length === 1)
@@ -293,7 +293,7 @@ export abstract class ListContainer<T,
{
// VALIDATION
if (first.source() !== this.end_.source() || last.source() !== this.end_.source())
throw new InvalidArgument("Parametric iterator is not this container's own.");
throw new InvalidArgument(`Error on std.${this.end_.source().constructor.name}.erase(): parametric iterator is not this container's own.`);

// FIND PREV AND NEXT
let prev: IteratorT = first.prev();
@@ -43,7 +43,7 @@ export abstract class UniqueMap<Key, T,
{
let it = this.find(key);
if (it.equals(this.end()) === true)
throw new OutOfRange("unable to find the matched key.");
throw new OutOfRange(`Error on std.${this.constructor.name}.get(): unable to find the matched key -> ${key}.`);

return it.second;
}
@@ -205,7 +205,7 @@ export abstract class UniqueMap<Key, T,
{
let it = this.find(key);
if (it.equals(this.end()) === true)
throw new OutOfRange("No such key exists.");
throw new OutOfRange(`Error on std.${this.constructor.name}.extract(): unable to find the matched key -> ${key}.`);

let ret: Entry<Key, T> = it.value;
this._Erase_by_range(it);
@@ -99,7 +99,7 @@ export abstract class UniqueSet<Key,
{
let it = this.find(key);
if (it.equals(this.end()) === true)
throw new OutOfRange("No such key exists.");
throw new OutOfRange(`Error on std.${this.constructor.name}.extract(): unable to find the matched key -> ${key}.`);

this._Erase_by_range(it);
return key;
@@ -82,20 +82,24 @@ export abstract class VectorContainer<T extends ElemT,
*/
public at(index: number): T
{
if (0 <= index && index < this.size())
return this.data_[index];
else
throw new OutOfRange("Target index is greater than Vector's size: " + index + ", " + this.size());
if (index < 0)
throw new OutOfRange(`Error on std.${this.constructor.name}.at(): parametric index is negative -> (index = ${index}).`);
else if (index >= this.size())
throw new OutOfRange(`Error on std.${this.constructor.name}.at(): parametric index is equal or greater than size -> (index = ${index}, size: ${this.size()}).`);

return this.data_[index];
}

/**
* @inheritDoc
*/
public set(index: number, val: T): void
{
if (index < 0 || index >= this.size())
throw new OutOfRange("Target index is greater than Vector's size: " + index + ", " + this.size());

if (index < 0)
throw new OutOfRange(`Error on std.${this.constructor.name}.set(): parametric index is negative -> (index = ${index}).`);
else if (index >= this.size())
throw new OutOfRange(`Error on std.${this.constructor.name}.set(): parametric index is equal or greater than size -> (index = ${index}, size: ${this.size()}).`);

this.data_[index] = val;
}

@@ -0,0 +1,92 @@
import { Vector, List, HashMap, ForwardList } from "../container";
import { SharedMutex } from "../thread/SharedMutex";
import { Semaphore } from "../experimental/thread/Semaphore";

import { Exception } from "../exception/Exception";
import { advance } from "../iterator";
import {
cyl_bessel_j, cyl_neumann,
cyl_bessel_i, cyl_bessel_k,
ellint_3,
hermite,
assoc_laguerre, assoc_legendre
} from "../numeric";

async function except(proc: Procedure): Promise<string>
{
try
{
await proc();
}
catch (exp)
{
if (exp instanceof Exception)
return ` ${exp.name} | ${exp.message} `;
}
return " NULL | NULL ";
}

export async function main(): Promise<string>
{
let ret: string = "# Exceptions\n" +
" name | message \n" +
"------|---------\n";

let v: Vector<number> = new Vector();
let fl: ForwardList<number> = new ForwardList();
let l: List<number> = new List();
let m: HashMap<number, number> = new HashMap();
let mtx: SharedMutex = new SharedMutex();
let sph: Semaphore<4> = new Semaphore(4);

let exceptions: string[] = [
// VECTOR
await except(() => { v.at(-1); }),
await except(() => { v.at(1); }),
await except(() => { v.set(-1, 4); }),
await except(() => { v.set(1, 4); }),
await except(() => { v.insert(new Vector<number>().begin(), 4); }),
await except(() => { v.insert(v.nth(-1), 4); }),
await except(() => { v.erase(new Vector<number>().begin()); }),
await except(() => { v.erase(v.nth(-1)); }),
await except(() => { v.erase(v.nth(4), v.nth(2)); }),

// LIST
await except(() => { l.insert(new List<number>().begin(), 4 ); }),
await except(() => { l.erase(new List<number>().begin()); }),

// ASSOCIATIVE
await except(() => { m.get(3); }),
await except(() => { m.extract(3); }),

// GLOBAL
await except(() => { advance(fl.begin(), -4); }),
await except(() => { cyl_bessel_j(2.3, -5); }),
await except(() => { cyl_bessel_j(2.3, 0); }),
await except(() => { cyl_neumann(2.3, -4); }),
await except(() => { cyl_bessel_i(-2.1, -0.5); }),
await except(() => { cyl_bessel_i(2.3, 0); }),
await except(() => { cyl_bessel_k(1, -0.3); }),
await except(() =>
{
let phi: number = 1.5;
let v: number = 1 / Math.pow(Math.sin(phi), 2) + 1;
ellint_3(0.5, v, phi);
}),
await except(() => { hermite(-5, 2); }),
await except(() => { assoc_laguerre(-1, -1, 1); }),
await except(() => { assoc_legendre(-1, -1, 0.5); }),
await except(() => { assoc_legendre(0.5, 0.5, 2.5); }),

// MUTEX
await except(() => mtx.unlock()),
await except(() => mtx.unlock_shared()),
await except(() => sph.release(0)),
await except(() => sph.release(5)),
await except(() => sph.release(2)),
];
ret += exceptions.join("\n") + "\n";

return ret;
}
type Procedure = () => void | Promise<void>;
@@ -0,0 +1,65 @@
import { FileSystem } from "./internal/FileSystem";

interface IModule
{
main(): Promise<string>;
}
const EXPORT_PATH: string = __dirname + "/../assets/benchmarks";

async function benchmark(feature: string): Promise<void>
{
//----
// MAIN PROCESS
//----
// LAZY CONSTRUCTION OF THE TARGET MODULE
let instance: IModule = await import(`${__dirname}/${feature}`);
let time: number = Date.now();

// CONTENT FROM THE SPEICAL MODULE
let content: string = await instance.main();
time = Date.now();

//----
// REPORT MEMORY USAGE
//----
let memory: NodeJS.MemoryUsage = global.process.memoryUsage();
let performance: string = "> ## Performancn"
+ `> - Elapsed time: ${time} ms\n`;

for (let key in memory)
{
let amount: number = memory[key as keyof NodeJS.MemoryUsage] / (10**6);
performance += `> - ${key}: ${amount} MB\n`;
}
content = performance + "\n\n" + content;

//----
// DO ARCHIVE
//----
await FileSystem.write(`${EXPORT_PATH}/${feature}.md`, content);
}

async function main(): Promise<void>
{
await FileSystem.mkdir(EXPORT_PATH);

if (process.argv[2])
{
// SPECIFIED FEATURE EXISTS
await benchmark(process.argv[2]);
}
else
{
// ITERATE ALL FEATURES
let directory: string[] = await FileSystem.dir(__dirname);
for (let file of directory)
{
if (file.substr(-3) !== ".js" || file === "index.js")
continue;

file = file.substr(0, file.length - 3);
await benchmark(file);
}
}
}
main();

0 comments on commit 816b9ea

Please sign in to comment.
You can’t perform that action at this time.