Add set.__gt__

This commit is contained in:
Aviv Palivoda
2019-02-08 17:50:36 +02:00
parent 0c737ae8d2
commit 358aa6b2c0
2 changed files with 34 additions and 0 deletions

View File

@@ -2,4 +2,9 @@ assert set([1,2]) == set([1,2])
assert not set([1,2,3]) == set([1,2])
assert set([1,2,3]) >= set([1,2])
assert set([1,2]) >= set([1,2])
assert not set([1,3]) >= set([1,2])
assert set([1,2,3]) > set([1,2])
assert not set([1,2]) > set([1,2])
assert not set([1,3]) > set([1,2])

View File

@@ -175,6 +175,34 @@ fn set_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.new_bool(true))
}
fn set_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [
(zelf, Some(vm.ctx.set_type())),
(other, Some(vm.ctx.set_type()))
]
);
let zelf_elements = get_elements(zelf);
let other_elements = get_elements(other);
if zelf_elements.len() <= other_elements.len() {
return Ok(vm.new_bool(false));
}
for element in other_elements.iter() {
match vm.call_method(zelf, "__contains__", vec![element.1.clone()]) {
Ok(value) => {
if !objbool::get_value(&value) {
return Ok(vm.new_bool(false));
}
}
Err(_) => return Err(vm.new_type_error("".to_string())),
}
}
Ok(vm.new_bool(true))
}
fn frozenset_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(o, Some(vm.ctx.frozenset_type()))]);
@@ -210,6 +238,7 @@ pub fn init(context: &PyContext) {
context.set_attr(&set_type, "__repr__", context.new_rustfunc(set_repr));
context.set_attr(&set_type, "__eq__", context.new_rustfunc(set_eq));
context.set_attr(&set_type, "__ge__", context.new_rustfunc(set_ge));
context.set_attr(&set_type, "__gt__", context.new_rustfunc(set_gt));
context.set_attr(&set_type, "__doc__", context.new_str(set_doc.to_string()));
context.set_attr(&set_type, "add", context.new_rustfunc(set_add));