pytest for my Single Linked list in python

Update 9-2-19 reformatted code, so it is in a html <pre><code> tag for easier styling and copy-pasting for users.

I'm modifying my single linked list yet again, and I realized, if someone did stumble across my list, and want to play with it , or use it to try something out,  they don't see what tests I did on it.

So before I go and change it up any more,  I'm going to put the test on here.  I don't think anyone is really seeing my blog, but maybe someday.  And one of the main reasons I did this blog, is because when I started learning-- it was, and still is, just terribly hard to find examples of code that I can look at and actually read.  Hoping to help someone who wants to learn this stuff.

pytest-installed in a virtual environment runs this fine.  I'm not sure why, but without the virtual environment it has issues running the test. If I find out why, I'll update.

But first!   A diagram!  Zed would remind me that the nodes are not 'inside' each other.  So if you get where I was going with the pictures,  they aren't inside each other.  :)


---code block--


from SLList import*

def test_pop():
    ##  remove the last item in the linked list
    colors = SLList()
    colors.push("Magenta")
    colors.push("Alizarin")
    colors.push("yellow")
  
    assert colors.pop() == "yellow"
    #assert colors.get(2) == None
    assert colors.get(1) == "Alizarin"
    assert colors.pop() == "Alizarin"
    #assert colors.get(2) == None
    assert colors.get(0) == "Magenta"
    assert colors.pop() == "Magenta"
    #assert colors.pop() == None
    assert colors.get(0) == None
    #print(zero)

def test_get():
    colors = SLList()
    assert colors.get(0) == None
    colors.push("Yellow")
    assert colors.get(0) == "Yellow"
    colors.push("Olive")
    assert colors.get(1) == "Olive"
    colors.push("Jade")
    assert colors.get(2) == "Jade"
    assert colors.get(3) == None
    assert colors.pop() == "Jade"
    assert colors.pop() == "Olive"
  
def test_insert():
    colors = SLList()
    colors.push("blue")
    assert colors.get(0) == "blue"
    colors.insert("new color", 0)
    assert colors.get(0) == "new color"
    assert colors.get(1) == "blue"
    colors.push("yellow")
    assert colors.count() == 3
    assert colors.get(2) == "yellow"
    colors.insert("orange", 2)
    assert colors.get(2) == "orange"
    assert colors.get(3) == "yellow"
    assert colors.count() == 4

def test_remove():
    colors = SLList()
    colors.push("blue") #0
    colors.push("yellow") #1
    colors.remove(0) #<--- fail.
    colors.push("green") #2
    colors.push("orange") #3
    colors.push("Trillium") #4
    colors.remove(1)
    assert colors.count() == 3
    assert colors.get(1) == "orange"
    assert colors.remove(9) == None
    colors.remove(0)
    assert colors.count() == 2
    assert colors.get(0) == "orange"
    assert colors.get(1) == "Trillium"
    assert colors.get(2) == None
    colors.insert("umber", 1)
    assert colors.get(1) == "umber"
    colors.remove(0)
    assert colors.count() == 2


def test_first():
    # copy and pasted remove test.  It does a ton of maniputaltion
    # to the list, so it's like an extra test to combine the two.
    colors = SLList()
    colors.push("blue") #0
    colors.push("yellow") #1
    colors.remove(0) #<--- fail.
    colors.push("green") #2
    colors.push("orange") #3
    colors.push("Trillium") #4
    colors.remove(1)
    assert colors.count() == 3
    assert colors.get(1) == "orange"
    assert colors.remove(9) == None
    colors.remove(0)
    assert colors.count() == 2
    assert colors.get(0) == "orange"
    assert colors.get(1) == "Trillium"
    assert colors.get(2) == None
    colors.insert("umber", 1)
    assert colors.get(1) == "umber"
    colors.remove(0)
    assert colors.count() == 2
    #assert colors.first == '[orange, None]' <-- returns address not string repr.
    #assert colors.first() == "orange" # <-- fail, returns umber
    # oh!  I had a remove 0,  so yes it should return umber
    assert colors.first() == "umber"

def test_last():
    # going to use the pop test to check this one
    colors = SLList()
    colors.push("Magenta")
    colors.push("Alizarin")
    colors.push("yellow")
  
    assert colors.pop() == "yellow"
    assert colors.last() == "Alizarin"
    assert colors.get(1) == "Alizarin"
    assert colors.pop() == "Alizarin"
    assert colors.last() == "Magenta"
    assert colors.get(0) == "Magenta"
    assert colors.pop() == "Magenta"
    assert colors.last() == None
    assert colors.get(0) == None

def test_unshift():
    # copy paste 'insert' test to try out this one:

    colors = SLList()
    colors.push("blue")
    assert colors.get(0) == "blue"
    colors.insert("new color", 0)
    assert colors.get(0) == "new color"
    assert colors.get(1) == "blue"
    colors.push("yellow")
    assert colors.count() == 3
    assert colors.get(2) == "yellow"
    colors.insert("orange", 2)
    assert colors.get(2) == "orange"
    assert colors.get(3) == "yellow"
    assert colors.count() == 4
    #########  testing unshift on manipulated list #####
    #!!!!   Using colors.unshift() in the assert removes item too
    assert colors.unshift() == "new color"
    #!!!! the assert removed the 0 node
    colors.unshift()
    assert colors.count() == 2
    assert colors.unshift() == "orange"
    assert colors.count() == 1
    assert colors.unshift() == "yellow"
    assert colors.count() == 0
    assert colors.unshift() == None

def test_non_string_types():
    ## store any valid object in this SLL
    colors = SLList()
    reds = ["Magenta", "lava", "fire", "passion"]
    blues = ["Ocean", "Lake Michigan", "mood", "Summer skys"]
    yellows = ["Daffodil", "school bus", "yield", "honey"]
    colors.push(reds)
    colors.push(blues)
    colors.push(yellows)

    assert colors.pop() == yellows
    #assert colors.get(2) == None
    assert colors.get(1) == blues
    assert colors.pop() == blues
    #assert colors.get(2) == None
    assert colors.get(0) == reds
    assert colors.pop() == reds
    #assert colors.pop() == None
    assert colors.get(0) == None
    #print(zero)

    dicts = SLList()
    nums = {'by two': [2,4,6], 'by three': [3,6,9], 'by four': [4, 8, 12]}
    tups = (1, 5, 20, 100)
    hats = ['bolar', 'top', 'helmet', 'cap', 'feathered']
    new_list = SLList()

    dicts.push(nums)
    dicts.push(tups)
    dicts.push(hats)
    dicts.push(new_list)
    
    assert dicts.get(0) == nums
    assert dicts.get(1) == tups
    assert dicts.get(2) == hats
    assert dicts.get(3) == new_list

    assert isinstance(dicts.pop(), SLList)



--end code block--



--

 


 



Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame