0

我从老师那里得到了这个测试课程,但我在理解如何实现我的课程以按此顺序创建自己时遇到了问题。

代码:

 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 所以我的问题是如何确保我可以按照代码测试的方式初始化我的包类?

4

1 回答 1

1

我相信这个想法是使用转换而不是简单的创建,沿着这些思路:

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以查看是否extend适合您的情况或需要使用其他内容。

于 2016-03-10T20:38:07.370 回答