mirror of https://github.com/gnosygnu/xowa
parent
12459429b4
commit
83cf992f48
@ -1,113 +0,0 @@
|
|||||||
/*
|
|
||||||
XOWA: the XOWA Offline Wiki Application
|
|
||||||
Copyright (C) 2012 gnosygnu@gmail.com
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU Affero General Public License as
|
|
||||||
published by the Free Software Foundation, either version 3 of the
|
|
||||||
License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU Affero General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Affero General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package gplx.langs.gfs; import gplx.*; import gplx.langs.*;
|
|
||||||
import org.junit.*;
|
|
||||||
public class GfsCore_tst {
|
|
||||||
@Before public void setup() {
|
|
||||||
core = GfsCore.new_();
|
|
||||||
core.AddObj(String_.Gfs, "String_");
|
|
||||||
core.AddObj(Int_.Gfs, "Int_");
|
|
||||||
} GfsCore core;
|
|
||||||
@Test public void Basic() { // String_.Len('abc') >> 3
|
|
||||||
tst_Msg
|
|
||||||
( msg_("String_").Subs_
|
|
||||||
( msg_("Len").Add("v", "abc"))
|
|
||||||
, 3);
|
|
||||||
}
|
|
||||||
@Test public void PrimitiveConversion() { // String_.Len('abc').Add(-3) >> 0
|
|
||||||
tst_Msg
|
|
||||||
( msg_("String_").Subs_
|
|
||||||
( msg_("Len").Add("v", "abc").Subs_
|
|
||||||
( msg_("Add").Add("operand", -3))
|
|
||||||
)
|
|
||||||
, 0);
|
|
||||||
}
|
|
||||||
// @Test public void Fail_notFound() { // String_.DoesNotExists
|
|
||||||
// tst_Err
|
|
||||||
// ( msg_("help").Add("", "String_.DoesNotExist")
|
|
||||||
// , GfsHelp.Err_Unhandled("String_", "DoesNotExist"));
|
|
||||||
// }
|
|
||||||
@Test public void Cmd() { // cmd
|
|
||||||
core.AddCmd(new GfsTest_cmd(), "testCmd");
|
|
||||||
tst_Msg
|
|
||||||
( msg_("testCmd").Add("s", "pass")
|
|
||||||
, "pass");
|
|
||||||
}
|
|
||||||
@Test public void EmptyMsg() {
|
|
||||||
tst_Msg
|
|
||||||
( msg_("")
|
|
||||||
, Gfo_invk_.Rv_unhandled);
|
|
||||||
}
|
|
||||||
// @Test public void Fail_argMissing() { // String_.Len()
|
|
||||||
// tst_String__Len_Err(msg_("Len"), GfsCtx.Err_KeyNotFound("v", "<<EMPTY>>"));
|
|
||||||
// }
|
|
||||||
// @Test public void Fail_argWrongKey() { // String_.Len(badKey='abc')
|
|
||||||
// tst_String__Len_Err(msg_("Len").Add("badKey", "abc"), GfsCtx.Err_KeyNotFound("v", "badKey;"));
|
|
||||||
// }
|
|
||||||
// @Test public void Fail_argExtraKey() { // String_.Len(v='abc' extraKey=1)
|
|
||||||
// tst_String__Len_Err(msg_("Len").Add("v", "abc").Add("extraKey", 1), GfsCtx.Err_KeyNotFound("v", "badKey;"));
|
|
||||||
// }
|
|
||||||
@Test public void Add_obj_deep() { // String_.Len(badKey='abc')
|
|
||||||
GfsCore_tst_nest obj1 = GfsCore_tst_nest.new_("1", "val1");
|
|
||||||
GfsCore_tst_nest obj1_1 = GfsCore_tst_nest.new_("1_1", "val2");
|
|
||||||
core.AddObj(obj1, "1");
|
|
||||||
core.AddDeep(obj1_1, "1", "1_1");
|
|
||||||
|
|
||||||
GfoMsg root = GfoMsg_.root_("1", "1_1", GfsCore_tst_nest.Prop2);
|
|
||||||
Object actl = core.ExecOne(GfsCtx.Instance, root);
|
|
||||||
Tfds.Eq("val2", actl);
|
|
||||||
}
|
|
||||||
void tst_String__Len_Err(GfoMsg m, Err expd) {
|
|
||||||
tst_Err(msg_("String_").Subs_(m), expd);
|
|
||||||
}
|
|
||||||
void tst_Err(GfoMsg msg, Err expd) {
|
|
||||||
GfoMsg root = msg;
|
|
||||||
GfsCtx ctx = GfsCtx.new_();
|
|
||||||
try {
|
|
||||||
core.ExecOne(ctx, root);
|
|
||||||
Tfds.Fail_expdError();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
Tfds.Eq_err(expd, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
GfoMsg msg_(String k) {return GfoMsg_.new_cast_(k);}
|
|
||||||
void tst_Msg(GfoMsg msg, Object expd) {
|
|
||||||
GfsCtx ctx = GfsCtx.new_();
|
|
||||||
Object actl = core.ExecOne(ctx, msg);
|
|
||||||
Tfds.Eq(expd, actl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
class GfsCore_tst_nest implements Gfo_invk, Gfo_invk_cmd_mgr_owner {
|
|
||||||
public Gfo_invk_cmd_mgr InvkMgr() {return invkMgr;} Gfo_invk_cmd_mgr invkMgr = Gfo_invk_cmd_mgr.new_();
|
|
||||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
|
||||||
if (ctx.Match(k, Prop1)) {return prop1;}
|
|
||||||
else if (ctx.Match(k, Prop2)) {return prop2;}
|
|
||||||
else if (ctx.Match(k, prop1)) {return this;}
|
|
||||||
else return invkMgr.Invk(ctx, ikey, k, m, this);
|
|
||||||
} public static final String Prop1 = "Prop1", Prop2 = "Prop2";
|
|
||||||
String prop1, prop2;
|
|
||||||
public static GfsCore_tst_nest new_(String prop1, String prop2) {
|
|
||||||
GfsCore_tst_nest rv = new GfsCore_tst_nest();
|
|
||||||
rv.prop1 = prop1; rv.prop2 = prop2;
|
|
||||||
return rv;
|
|
||||||
} GfsCore_tst_nest() {}
|
|
||||||
}
|
|
||||||
class GfsTest_cmd implements Gfo_invk {
|
|
||||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {return m.ReadStr("s");}
|
|
||||||
}
|
|
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
XOWA: the XOWA Offline Wiki Application
|
||||||
|
Copyright (C) 2012 gnosygnu@gmail.com
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Affero General Public License as
|
||||||
|
published by the Free Software Foundation, either version 3 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package gplx.xowa.wikis.tdbs.hives; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*; import gplx.xowa.wikis.tdbs.*;
|
||||||
|
import gplx.core.lists.*;
|
||||||
|
public class Xowd_regy_mgr_ {
|
||||||
|
public static int FindSlot(ComparerAble comparer, Object[] ary, Object itm) {if (itm == null) throw Err_.new_null();
|
||||||
|
int aryLen = ary.length;
|
||||||
|
switch (aryLen) {
|
||||||
|
case 0: throw Err_.new_wo_type("ary cannot have 0 itms");
|
||||||
|
case 1: return 0;
|
||||||
|
}
|
||||||
|
int lo = -1, hi = aryLen - 1; // NOTE: -1 is necessary; see test
|
||||||
|
int curPos = (hi - lo) / 2;
|
||||||
|
int delta = 1;
|
||||||
|
while (true) {
|
||||||
|
Object curSeg = ary[curPos];
|
||||||
|
int comp = curSeg == null ? CompareAble_.More : comparer.compare(itm, curSeg); // nulls should only happen for lastAry
|
||||||
|
// if (dbg) {
|
||||||
|
// Tfds.Write(curPos, itm.toString(), comp, comp.toString(), curSeg.toString());
|
||||||
|
// }
|
||||||
|
if (comp == CompareAble_.Same) return curPos;
|
||||||
|
else if (comp > CompareAble_.Same) {lo = curPos; delta = 1;}
|
||||||
|
else if (comp < CompareAble_.Same) {hi = curPos; delta = -1;}
|
||||||
|
int dif = hi - lo;
|
||||||
|
if (dif == 1 || dif == 0) return hi; // NOTE: can be 0 when ary.length == 1 || 2; also, sometimes 0 in some situations
|
||||||
|
else curPos += (dif / 2) * delta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue