Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Add builtin sorted (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
S-YOU authored and trotterdylan committed Jan 13, 2017
1 parent 5497adb commit 384954e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 38 deletions.
14 changes: 14 additions & 0 deletions runtime/builtin_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,19 @@ func builtinRepr(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
return s.ToObject(), nil
}

func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
// TODO: Support (cmp=None, key=None, reverse=False)
if raised := checkFunctionArgs(f, "sorted", args, ObjectType); raised != nil {
return nil, raised
}
result, raised := ListType.Call(f, Args{args[0]}, nil)
if raised != nil {
return nil, raised
}
toListUnsafe(result).Sort(f)
return result, nil
}

func builtinUniChr(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "unichr", args, IntType); raised != nil {
return nil, raised
Expand Down Expand Up @@ -585,6 +598,7 @@ func init() {
"print": newBuiltinFunction("print", builtinPrint).ToObject(),
"range": newBuiltinFunction("range", builtinRange).ToObject(),
"repr": newBuiltinFunction("repr", builtinRepr).ToObject(),
"sorted": newBuiltinFunction("sorted", builtinSorted).ToObject(),
"True": True.ToObject(),
"unichr": newBuiltinFunction("unichr", builtinUniChr).ToObject(),
}
Expand Down
9 changes: 9 additions & 0 deletions runtime/builtin_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ func TestBuiltinFuncs(t *testing.T) {
{f: "repr", args: wrapArgs(NewUnicode("abc")), want: NewStr("u'abc'").ToObject()},
{f: "repr", args: wrapArgs(newTestTuple("foo", "bar")), want: NewStr("('foo', 'bar')").ToObject()},
{f: "repr", args: wrapArgs("a", "b", "c"), wantExc: mustCreateException(TypeErrorType, "'repr' requires 1 arguments")},
{f: "sorted", args: wrapArgs(NewList()), want: NewList().ToObject()},
{f: "sorted", args: wrapArgs(newTestList("foo", "bar")), want: newTestList("bar", "foo").ToObject()},
{f: "sorted", args: wrapArgs(newTestList(true, false)), want: newTestList(false, true).ToObject()},
{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), want: newTestRange(4).ToObject()},
{f: "sorted", args: wrapArgs(newTestRange(100)), want: newTestRange(100).ToObject()},
{f: "sorted", args: wrapArgs(newTestTuple(1, 2, 0, 3)), want: newTestRange(4).ToObject()},
{f: "sorted", args: wrapArgs(newTestDict("foo", 1, "bar", 2)), want: newTestList("bar", "foo").ToObject()},
{f: "sorted", args: wrapArgs(1), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
{f: "sorted", args: wrapArgs(newTestList("foo", "bar"), 2), wantExc: mustCreateException(TypeErrorType, "'sorted' requires 1 arguments")},
{f: "unichr", args: wrapArgs(0), want: NewUnicode("\x00").ToObject()},
{f: "unichr", args: wrapArgs(65), want: NewStr("A").ToObject()},
{f: "unichr", args: wrapArgs(0x120000), wantExc: mustCreateException(ValueErrorType, "unichr() arg not in range(0x10ffff)")},
Expand Down
99 changes: 61 additions & 38 deletions testing/builtin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,19 @@
assert not callable((1, 2, 3))
assert not callable({'foo': 1, 'bar': 2})

assert callable(lambda x: x+1)
assert callable(lambda x: x + 1)


def foo(x):
pass
pass

assert callable(foo)


class bar(object):
def __call__(self, *args, **kwargs):
pass

def __call__(self, *args, **kwargs):
pass

assert callable(bar)
assert callable(bar())
Expand All @@ -108,54 +111,66 @@ def __call__(self, *args, **kwargs):
assert cmp(3, 3) == 0
assert cmp(5, 4) == 1


class Lt(object):
def __init__(self, x):
self.lt_called = False
self.x = x

def __lt__(self, other):
self.lt_called = True
return self.x < other.x
def __init__(self, x):
self.lt_called = False
self.x = x

def __lt__(self, other):
self.lt_called = True
return self.x < other.x


class Eq(object):
def __init__(self, x):
self.eq_called = False
self.x = x

def __eq__(self, other):
self.eq_called = True
return self.x == other.x
def __init__(self, x):
self.eq_called = False
self.x = x

def __eq__(self, other):
self.eq_called = True
return self.x == other.x


class Gt(object):
def __init__(self, x):
self.gt_called = False
self.x = x

def __gt__(self, other):
self.gt_called = True
return self.x > other.x
def __init__(self, x):
self.gt_called = False
self.x = x

def __gt__(self, other):
self.gt_called = True
return self.x > other.x


class RichCmp(Lt, Eq, Gt):
def __init__(self, x):
self.x = x

def __init__(self, x):
self.x = x


class Cmp(object):
def __init__(self, x):
self.cmp_called = False
self.x = x

def __cmp__(self, other):
self.cmp_called = True
if self.x < other.x:
return -1
elif self.x > other.x:
return 1
else:
return 0

def __init__(self, x):
self.cmp_called = False
self.x = x

def __cmp__(self, other):
self.cmp_called = True
if self.x < other.x:
return -1
elif self.x > other.x:
return 1
else:
return 0


class NoCmp(object):
def __init__(self, x):
self.x = x

def __init__(self, x):
self.x = x

# Test 3-way compare in terms of rich compare.

Expand Down Expand Up @@ -231,3 +246,11 @@ def __init__(self, x):
a, b = NoCmp(1), Cmp(2)
assert cmp(a, b) == -1
assert b.cmp_called

# Test sorted

assert sorted([3, 2, 4, 1]) == [1, 2, 3, 4]
assert sorted([]) == []
assert sorted(["a", "e", "c", "b"]) == ["a", "b", "c", "e"]
assert sorted((3, 1, 5, 2, 4)) == [1, 2, 3, 4, 5]
assert sorted({"foo": 1, "bar": 2}) == ["bar", "foo"]

0 comments on commit 384954e

Please sign in to comment.