我从老师那里得到了这个测试课程,我有一个问题,那就是如何实现我的课堂,按照这个顺序创建自己。
代码:
t5: BOOLEAN
local
bag: MY_BAG[STRING]
bag3: like bag2
bag4: MY_BAG[STRING]
do
comment("t5:test add_all, remove all, remove")
bag := <<["nuts", 4], ["bolts", 11], ["hammers", 5]>> -- how to implement it to allow this?
bag3 := <<["nuts", 2], ["bolts", 6], ["hammers", 5]>>
-- add_all
bag3.add_all (bag1)
Result := bag3 |=| bag
check Result end
-- remove_all
bag3.remove_all (bag1)
Result := bag3 |=| bag2 and bag3.total = 13
check Result end
--remove
bag4 := <<["nuts", 2], ["bolts", 6]>>
bag3.remove ("hammers", 5)
Result := bag3 |=| bag4
end
setup -- inherit from ES_TEST redefine setup end
-- this runs before every test
do
bag1 := <<["nuts", 2], ["bolts", 5]>>
bag2 := <<["nuts", 2], ["bolts", 6], ["hammers", 5]>>
end目前,当我用这种测试用例编译时,它会抛出编译器错误,说创建是不适当的,不兼容的。
我的构造函数目前是一个空的HASH_TABLE,所以我的问题是,如何确保我能够以代码测试的方式初始化包类?
发布于 2016-03-10 20:38:07
我相信这个想法是使用转换,而不是简单的创造,类似于这样的东西:
class MY_BAG [G]
create
make
convert
make ({ARRAY [TUPLE [G, INTEGER]]})
feature {NONE} -- Creation
make (a: ARRAY [TUPLE [value: G; key: INTEGER]])
do
create storage.make (a.count)
across
a as c
loop
storage.extend (c.item.value, c.item.key)
end
end
feature {NONE} -- Storage
storage: HASH_TABLE [G, INTEGER]
end您还可能希望查看类HASH_TABLE中的note子句,以确定extend是否适合您的情况,还是需要使用其他东西。
https://stackoverflow.com/questions/35921636
复制相似问题