I used a solution that I happened to already have on my laptop on an exam. Did I cheat? In a programming in-class test, one of the problems was to write up a certain famous program in Maple. However, I had already written up this program prior to the test just as a hobby, and it was in a folder on my desktop [we were using our own laptops, no internet access]. I just copied that code into a new file, cleaned it up, and uploaded this file to the course website. I left an hour before anybody else [the exam lasted 2 hours, and I sat around pretending to be busy for 45 minutes before I left the class and went to the canteen]. 

Question: Is this cheating? Self-plagiarism? Have I done anything wrong here?
Should I admit this to my TA?
I would say no, unless this was explicitly prohibited beforehand. Look, what's the purpose of a test?  To gauge the extent to which one has comprehended the material. The fact that you did the work indicates you do. Hell, the fact you did it on your own for fun deserves recognition. 

What's the point of re-doing the work while sitting in class during a test?  Worse yet, suppose you did and made a mistake due to nerves/time pressure.  You still did and comprehended the material, thus you pass.  

Others will disagree, but in my mind, I just want to know if you do or do not understand the material.