Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
grumpy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
grumpy
Commits
90786d22
Commit
90786d22
authored
Jan 18, 2017
by
Dylan Trotter
Committed by
GitHub
Jan 18, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement xrange.__len__. (#159)
parent
468ce7d7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
0 deletions
+20
-0
runtime/range.go
runtime/range.go
+6
-0
runtime/range_test.go
runtime/range_test.go
+14
-0
No files found.
runtime/range.go
View file @
90786d22
...
@@ -182,6 +182,11 @@ func xrangeIter(f *Frame, o *Object) (*Object, *BaseException) {
...
@@ -182,6 +182,11 @@ func xrangeIter(f *Frame, o *Object) (*Object, *BaseException) {
return
&
(
&
rangeIterator
{
Object
{
typ
:
rangeIteratorType
},
r
.
start
,
r
.
stop
,
r
.
step
})
.
Object
,
nil
return
&
(
&
rangeIterator
{
Object
{
typ
:
rangeIteratorType
},
r
.
start
,
r
.
stop
,
r
.
step
})
.
Object
,
nil
}
}
func
xrangeLen
(
f
*
Frame
,
o
*
Object
)
(
*
Object
,
*
BaseException
)
{
r
:=
toXRangeUnsafe
(
o
)
return
NewInt
((
r
.
stop
-
r
.
start
)
/
r
.
step
)
.
ToObject
(),
nil
}
func
xrangeNew
(
f
*
Frame
,
_
*
Type
,
args
Args
,
_
KWArgs
)
(
*
Object
,
*
BaseException
)
{
func
xrangeNew
(
f
*
Frame
,
_
*
Type
,
args
Args
,
_
KWArgs
)
(
*
Object
,
*
BaseException
)
{
expectedTypes
:=
[]
*
Type
{
IntType
,
IntType
,
IntType
}
expectedTypes
:=
[]
*
Type
{
IntType
,
IntType
,
IntType
}
argc
:=
len
(
args
)
argc
:=
len
(
args
)
...
@@ -229,6 +234,7 @@ func initXRangeType(map[string]*Object) {
...
@@ -229,6 +234,7 @@ func initXRangeType(map[string]*Object) {
xrangeType
.
flags
&^=
typeFlagBasetype
xrangeType
.
flags
&^=
typeFlagBasetype
xrangeType
.
slots
.
GetItem
=
&
binaryOpSlot
{
xrangeGetItem
}
xrangeType
.
slots
.
GetItem
=
&
binaryOpSlot
{
xrangeGetItem
}
xrangeType
.
slots
.
Iter
=
&
unaryOpSlot
{
xrangeIter
}
xrangeType
.
slots
.
Iter
=
&
unaryOpSlot
{
xrangeIter
}
xrangeType
.
slots
.
Len
=
&
unaryOpSlot
{
xrangeLen
}
xrangeType
.
slots
.
New
=
&
newSlot
{
xrangeNew
}
xrangeType
.
slots
.
New
=
&
newSlot
{
xrangeNew
}
xrangeType
.
slots
.
Repr
=
&
unaryOpSlot
{
xrangeRepr
}
xrangeType
.
slots
.
Repr
=
&
unaryOpSlot
{
xrangeRepr
}
}
}
runtime/range_test.go
View file @
90786d22
...
@@ -85,6 +85,20 @@ func TestXRangeGetItem(t *testing.T) {
...
@@ -85,6 +85,20 @@ func TestXRangeGetItem(t *testing.T) {
}
}
}
}
func
TestXRangeLen
(
t
*
testing
.
T
)
{
cases
:=
[]
invokeTestCase
{
{
args
:
wrapArgs
(
newTestXRange
(
10
)),
want
:
NewInt
(
10
)
.
ToObject
()},
{
args
:
wrapArgs
(
newTestXRange
(
10
,
12
)),
want
:
NewInt
(
2
)
.
ToObject
()},
{
args
:
wrapArgs
(
newTestXRange
(
5
,
16
,
5
)),
want
:
NewInt
(
3
)
.
ToObject
()},
{
args
:
wrapArgs
(
newTestXRange
(
5
,
-
2
,
-
3
)),
want
:
NewInt
(
3
)
.
ToObject
()},
}
for
_
,
cas
:=
range
cases
{
if
err
:=
runInvokeMethodTestCase
(
xrangeType
,
"__len__"
,
&
cas
);
err
!=
""
{
t
.
Error
(
err
)
}
}
}
func
TestXRangeNew
(
t
*
testing
.
T
)
{
func
TestXRangeNew
(
t
*
testing
.
T
)
{
fun
:=
newBuiltinFunction
(
"TestXRangeNew"
,
func
(
f
*
Frame
,
args
Args
,
_
KWArgs
)
(
*
Object
,
*
BaseException
)
{
fun
:=
newBuiltinFunction
(
"TestXRangeNew"
,
func
(
f
*
Frame
,
args
Args
,
_
KWArgs
)
(
*
Object
,
*
BaseException
)
{
xrange
,
raised
:=
xrangeType
.
Call
(
f
,
args
,
nil
)
xrange
,
raised
:=
xrangeType
.
Call
(
f
,
args
,
nil
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment