Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:25:13+00:00 2026-05-15T14:25:13+00:00

I am developing a simulator for a machine. It reads a file in, stores

  • 0

I am developing a simulator for a machine. It reads a file in, stores it in a string (as a binary number) to an array that is supposed to simulate the memory of a computer. Then it gets passed to an interpreter that takes gets the first element of the array, and based on the first 4 characters it gets sent to another class.

An example of one of those classes is below:

public class Add {
private String cell;

Add() {
    cell = null;
}

/**
 * Method that performs the operation add.
 */
public void addition() {
    // TODO Figure out 2's complement.
    Loader instance = Loader.Instance();
    cell = instance.getCell();

    Integer DR1num = Integer.parseInt(cell.substring(4, 6), 2);
    Integer SR1num = Integer.parseInt(cell.substring(7, 9), 2);

    if (cell.charAt(10) == '0') {
        Integer SR2num = Integer.parseInt(cell.substring(13, 15), 2);
        Integer addedval = instance.getRegister(SR1num)
                + instance.getRegister(SR2num);
        instance.setCCR(addedval);
        instance.setRegister(DR1num, addedval);
    } else if (cell.charAt(10) == '1') {
        Integer immediate = Integer.parseInt(cell.substring(11, 15), 2);
        Integer addedval = instance.getRegister(SR1num) + immediate;
        instance.setCCR(addedval);
        instance.setRegister(DR1num, addedval);
    } else {
        System.err.println("Invalid Char at Position 10. Expected 0 or 1");
    }

    instance.incrementPC();

}

public String getcell() {
    return cell;
}

}

Variable Descriptions:
instance is the singleton
cell is the binary string that is stored in the memory array
DR1num is Destination Register. (aka R0, R1, … R7)
SR1num is Source Register

What this operation does is takes either two Source Registers and adds them together (then sets the Condition Code Register if it is positive, negative, or zero), or takes a Source Register and an immediate value and adds those two together. The result is stored in the Destination Register.

I chose to use a singleton class because I need the Memory, General Purpose Registers, Condition Code Register, and Program Counter to be in sync throughout the entire program. The problem that this presents is that I have no idea how to test my program (preferably with jUnit).

The only way I can think of testing the program is by writing a bunch of individual files for the program to read in. Except this means that instead of testing only one part of my program, at a minimum it tests three classes. The class that reads in the program and contains all the methods for the singleton, the class that determines which class to call, and finally the class that actually executes that portion (such as Add).

Does anyone have any suggestions for a better way to test?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:25:14+00:00Added an answer on May 15, 2026 at 2:25 pm

    The way to avoid this is traditionally to use inversion of control (IoC) and inject an instance of your singleton class into the client class. Your client class is provided with all the dependent classes it requires to function, and not concerned with the lifecycle of the dependent classes.

    Spring is the common Java framework used to do this, but you can inject simply by constructing your class with a reference to the singleton (it’s that simple).

    If the singleton class implements an interface then testing is made easier since you can inject mock variants rather than the real implementation, and build your mock to provide suitable test data for your tests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an iPad application that I have been developing on simulator just fine
I'm developing an iOS app. Its working properly on simulator. I'm using phonegap and
I'm developing a Silverlight application where I want to simulate a console. There are
I'm developing a firefox extension and i want to simulate :hover state of an
I'm developing an app with SDK 3.1.2 and it runs in the simulator, but
I am developing BlackBerry application that connects to the web service. When I developed
I am developing phonegap app in iphone,after successfully viewing pdf file (from server) on
While developing part of a simulator I came across the following problem. Consider a
I am developing an application using titanium mobile. On the simulator, the app works
So I'm developing in Xcode for iOS, and EVERY APP that I run in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.