001    // This file is part of the program FRYSK.
002    //
003    // Copyright 2007, Red Hat Inc.
004    //
005    // FRYSK is free software; you can redistribute it and/or modify it
006    // under the terms of the GNU General Public License as published by
007    // the Free Software Foundation; version 2 of the License.
008    //
009    // FRYSK is distributed in the hope that it will be useful, but
010    // WITHOUT ANY WARRANTY; without even the implied warranty of
011    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012    // General Public License for more details.
013    // 
014    // You should have received a copy of the GNU General Public License
015    // along with FRYSK; if not, write to the Free Software Foundation,
016    // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
017    // 
018    // In addition, as a special exception, Red Hat, Inc. gives You the
019    // additional right to link the code of FRYSK with code not covered
020    // under the GNU General Public License ("Non-GPL Code") and to
021    // distribute linked combinations including the two, subject to the
022    // limitations in this paragraph. Non-GPL Code permitted under this
023    // exception must only link to the code of FRYSK through those well
024    // defined interfaces identified in the file named EXCEPTION found in
025    // the source code files (the "Approved Interfaces"). The files of
026    // Non-GPL Code may instantiate templates or use macros or inline
027    // functions from the Approved Interfaces without causing the
028    // resulting work to be covered by the GNU General Public
029    // License. Only Red Hat, Inc. may make changes or additions to the
030    // list of Approved Interfaces. You must obey the GNU General Public
031    // License in all respects for all of the FRYSK code and other code
032    // used in conjunction with FRYSK except the Non-GPL Code covered by
033    // this exception. If you modify this file, you may extend this
034    // exception to your version of the file, but you are not obligated to
035    // do so. If you do not wish to provide this exception without
036    // modification, you must delete this exception statement from your
037    // version and license this file solely under the GPL without
038    // exception.
039    
040    package frysk.testbed;
041    
042    import frysk.junit.TestCase;
043    
044    /**
045     * Check that LocalMemory addresses are pointing where expected.
046     */
047    public class TestLocalMemory
048        extends TestCase
049    {
050        /**
051         * Check that the stack address changes as new stack frames are
052         * created.
053         */
054        public void testStackChanging() {
055            // Capture a sequence of stack addresses.
056            class Stacks implements LocalMemory.StackBuilder {
057                    int level = 0;
058                    long[] addresses = new long[2];
059                    public void stack(long addr, byte[] bytes) {
060                        if (level >= addresses.length)
061                            return;
062                        addresses[level++] = addr;
063                        LocalMemory.constructStack(this);
064                    }
065            }
066            Stacks stacks = new Stacks();
067            LocalMemory.constructStack(stacks);
068            assertEquals("level", stacks.addresses.length, stacks.level);
069            for (int i = 0; i < stacks.addresses.length - 1; i++) {
070                for (int j = i + 1; j < stacks.addresses.length; j++) {
071                    assertTrue("stack address " + i + " and " + j,
072                               stacks.addresses[i] != stacks.addresses[j]);
073                }
074            }
075        }
076    
077        /**
078         * Check that the stack contents are as expected.
079         */
080        public void testStackContents() {
081            LocalMemory.constructStack(new LocalMemory.StackBuilder() {
082                    public void stack(long addr, byte[] bytes) {
083                        // The stack contains a copy of the data.
084                        assertEquals("bytes", LocalMemory.getDataBytes(), bytes);
085                        // The stack isn't the same address as the data or code.
086                        assertTrue("data", LocalMemory.getDataAddr() != addr);
087                        assertTrue("code", LocalMemory.getCodeAddr() != addr);
088                    }
089                });
090        }
091    
092        /**
093         * Check that at least the first data byte is as expected.
094         */
095        public void testDataContents() {
096            byte[] bytes = LocalMemory.getDataBytes();
097            assertEquals("data byte[0]", 43, bytes[0]);
098        }
099    }