123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import API.Utils;
- import org.junit.After;
- import org.junit.AfterClass;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import tools.mips.Memory;
- /**
- *
- * @author EUGENIO CARVALHO
- */
- public class MemoryJUnitTest {
- private Memory memory;
- public MemoryJUnitTest() {
- }
- @BeforeClass
- public static void setUpClass() {
- }
- @AfterClass
- public static void tearDownClass() {
- }
- @Before
- public void setUp() {
- memory = new Memory(16 * 1024);
- }
- @After
- public void tearDown() {
- }
- @Test
- public void WriteByte() throws Exception {
- String bd = "00000010";
- memory.WB(bd);
- Assert.assertEquals(bd, memory.RB(0));
- }
- @Test
- public void WriteByteAddressed() throws Exception {
- String bd = "00000010";
- Long address = 16L;
- memory.WB(address, bd);
- Assert.assertEquals(bd, memory.RB(address));
- }
- //
- // @Test
- // public void WriteMultByteAddressed() throws Exception {
- // Long value = 14030L;
- //
- // Long address = 16L;
- // // Escreve 4 bytes a partir do endereco 16
- // memory.W(address, Long.toBinaryString(value), 4);
- //
- // // Lê 4 bytes a partir do endereco 16 -> converte binario para long
- // Assert.assertEquals(value, memory.ReadLong(address, 4));
- // }
- @Test
- public void WriteNegativeValueAddressed() throws Exception {
- String bin = Long.toBinaryString(3L);
- printOneAndTwosComplement(Utils.Pad(32, bin));
- bin = Long.toBinaryString(-3L);
- printOneAndTwosComplement(Utils.Pad(32, bin));
- // System.out.println("2c:" + findTwoscomplement(new StringBuffer(Long.toBinaryString(value))));
- // convertStringToDecimal(
- // findTwoscomplement(new StringBuffer(Long.toBinaryString(value)))
- // );
- // Escreve 4 bytes a partir do endereco 16
- // memory.W(address, Long.toBinaryString(value), 4);
- // Lê 4 bytes a partir do endereco 16 -> converte binario para long
- // Assert.assertEquals(value, memory.ReadLong(address, 4));
- }
- @Test
- public void WriteInvalidAddress() throws Exception {
- boolean result = false;
- try {
- String bd = "00000010";
- Long address = 17000L;
- memory.WB(address, bd);
- } catch (Exception e) {
- result = true;
- }
- Assert.assertTrue(result);
- }
- // Print 1's and 2's complement of binary number
- // represented by "bin"
- public static void printOneAndTwosComplement(String bin) {
- int n = bin.length();
- int i;
- String ones = "";
- StringBuilder twos;
- // for ones complement flip every bit
- for (i = 0; i < n; i++) {
- ones += flip(bin.charAt(i));
- }
- System.out.println("Bin b:" + bin);
- System.out.println("One b:" + ones);
- twos = new StringBuilder(Long.toBinaryString(Long.parseLong(ones, 2) + 1L));
- System.out.println("Two b:" + twos);
- // for (i = n - 1; i >= 0; i--) {
- for (i = 0; i < n; i++) {
- if (ones.charAt(i) == '1') {
- twos.setCharAt(i, '0');
- } else {
- twos.setCharAt(i, '1');
- break;
- }
- }
- System.out.println("Two a:" + twos);
- // If No break : all are 1 as in 111 or 11111;
- // in such case, add extra 1 at beginning
- // if (i == -1) {
- // twos = '1' + twos;
- // }
- // System.out.println("1's complement: " + ones);
- // System.out.println("2's complement: " +);
- }
- // Returns '0' for '1' and '1' for '0'
- public static char flip(char c) {
- return (c == '0') ? '1' : '0';
- }
- //
- // static String findTwoscomplement(StringBuffer str) {
- // int n = str.length();
- //
- // // Traverse the string to get first '1' from
- // // the last of string
- // int i;
- // for (i = n - 1; i >= 0; i--) {
- // if (str.charAt(i) == '1') {
- // break;
- // }
- // }
- //
- // // If there exists no '1' concat 1 at the
- // // starting of string
- // if (i == -1) {
- // return "1" + str;
- // }
- //
- // // Continue traversal after the position of
- // // first '1'
- // for (int k = i - 1; k >= 0; k--) {
- // //Just flip the values
- // if (str.charAt(k) == '1') {
- // str.replace(k, k + 1, "0");
- // } else {
- // str.replace(k, k + 1, "1");
- // }
- // }
- //
- // // return the modified string
- // return str.toString();
- // }
- //
- // public static void convertStringToDecimal(String binary) {
- // int decimal = 0;
- //
- // int power = 0;
- //
- // if (binary.charAt(0) == '1' && binary.length() == 32) {
- //
- // StringBuilder builder = new StringBuilder();
- //
- // for (int i = 0; i < binary.length(); i++) {
- //
- // builder.append((binary.charAt(i) == '1' ? '0' : '1'));
- //
- // }
- //
- // while (binary.length() > 0) {
- //
- // int temp = Integer
- // .parseInt(builder.charAt((binary.length()) - 1) + "");
- // decimal += temp * Math.pow(2, power++);
- // binary = binary.substring(0, binary.length() - 1);
- //
- // }
- //
- // System.out.println((decimal + 1) * (-1));
- //
- // } else {
- //
- // while (binary.length() > 0) {
- // int temp = Integer
- // .parseInt(binary.charAt((binary.length()) - 1) + "");
- // decimal += temp * Math.pow(2, power++);
- // binary = binary.substring(0, binary.length() - 1);
- // }
- //
- // System.out.println(decimal);
- //
- // }
- // }
- // public static String TwosCompliment(String bin) {
- // String twos = "", ones = "";
- // System.out.println("bin:" + bin);
- //
- // for (int i = 0; i < bin.length(); i++) {
- // ones += flip(bin.charAt(i));
- // }
- //
- // int number0 = (int) Long.parseLong(ones, 2);
- //
- // System.out.println("number0:" + number0);
- //
- // StringBuilder builder = new StringBuilder(ones);
- // boolean b = false;
- //
- // for (int i = ones.length() - 1; i > 0; i--) {
- // if (ones.charAt(i) == '1') {
- // builder.setCharAt(i, '0');
- // } else {
- // builder.setCharAt(i, '1');
- // b = true;
- // break;
- // }
- // }
- // if (!b) {
- // builder.append("1", 0, 7);
- // }
- //
- // twos = builder.toString();
- //
- // return twos;
- // }
- }
|