0

I'm given this test class from the teacher and I have an issue understanding how to implement my class to create itself in this order.

code:

 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

Currently when I compile with these kind of test cases it throws compiler errors saying creation is improper, incompatible

My constructors make an empty HASH_TABLE at the moment so my question is how do I make sure I can initialize my bag class in the way the code is testing it?

geforce
  • 61
  • 6

1 Answers1

1

I believe the idea is to use conversion instead of plain creation, something along those lines:

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

You may also wish to look at the note clause in the class HASH_TABLE to see whether extend is suitable for your case or something else needs to be used.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35