• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyberZHG / MIXAL / 20697838146

10 Jan 2021 08:00AM UTC coverage: 98.174% (-0.2%) from 98.385%
20697838146

push

github

CyberZHG
Add examples in 1.3.2

25 of 29 new or added lines in 4 files covered. (86.21%)

4 existing lines in 1 file now uncovered.

3548 of 3614 relevant lines covered (98.17%)

19275.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.35
/src/io.cpp
1
#include <iostream>
2
#include <cstdlib>
3
#include <cmath>
4
#include <algorithm>
5
#include <sstream>
6
#include "io.h"
7

8
namespace mixal {
9

10
IODevice::IODevice(int32_t blockSize, bool allowRead, bool allowWrite) : _type(IODeviceType::TAPE),
36✔
11
        _blockSize(blockSize), _allowRead(allowRead), _allowWrite(allowWrite),
36✔
12
        _timestamp(), _readyRate(1.0) {}
36✔
13

14
bool IODevice::ready(int32_t timestamp) {
486✔
15
    int32_t elapsed = std::max(0, timestamp - _timestamp);
486✔
16
    _timestamp = timestamp;
486✔
17
    double r = static_cast<double>(rand()) / RAND_MAX;
486✔
18
    double successRate = 1.0 - pow(1.0 - _readyRate, elapsed);
486✔
19
    return r <= successRate;
486✔
20
}
21

22
IODeviceStorage::IODeviceStorage(int32_t storageSize) : IODevice(100, true, true),
36✔
23
        _status(IODeviceStatus::READY), _address(0), _locator(0), _memory(nullptr),
36✔
24
        _buffer(100), _storage(storageSize) {}
180✔
25

26
bool IODeviceStorage::ready(int32_t elapsed) {
486✔
27
    bool state = IODevice::ready(elapsed);
486✔
28
    if (state) {
486✔
29
        if (_status == IODeviceStatus::BUSY_READ) {
94✔
30
            doRead();
5✔
31
        } else if (_status == IODeviceStatus::BUSY_WRITE) {
89✔
32
            doWrite();
60✔
33
        }
34
    }
35
    return state;
486✔
36
}
37

38
void IODeviceStorage::read(ComputerWord* memory, int32_t address) {
5✔
39
    _status = IODeviceStatus::BUSY_READ;
5✔
40
    _address = address;
5✔
41
    _memory = memory;
5✔
42
    for (int i = 0; i < _blockSize; ++i) {
337✔
43
        _buffer[i] = _storage[_locator + i];
332✔
44
    }
45
    ready(_timestamp);
5✔
46
}
5✔
47

48
void IODeviceStorage::write(const ComputerWord* memory, int32_t address) {
60✔
49
    _status = IODeviceStatus::BUSY_WRITE;
60✔
50
    for (int i = 0; i < _blockSize; ++i) {
1,680✔
51
        _buffer[i] = memory[address + i];
1,620✔
52
    }
53
    ready(_timestamp);
60✔
54
}
60✔
55

56
void IODeviceStorage::doRead() {
5✔
57
    for (int i = 0; i < _blockSize; ++i) {
337✔
58
        _memory[_address + i] = _buffer[i];
332✔
59
    }
60
    _status = IODeviceStatus::READY;
5✔
61
}
5✔
62

63
void IODeviceStorage::doWrite() {
60✔
64
    for (int i = 0; i < _blockSize; ++i) {
1,680✔
65
        _storage[_locator + i] = _buffer[i];
1,620✔
66
    }
67
    _status = IODeviceStatus::READY;
60✔
68
}
60✔
69

70
IODeviceTape::IODeviceTape(int32_t storageSize) : IODeviceStorage(storageSize) {
10✔
71
    _type = IODeviceType::TAPE;
10✔
72
    _readyRate = 0.1;
10✔
73
}
10✔
74

75
void IODeviceTape::control(int32_t operation) {
10✔
76
    if (operation == 0) {
10✔
77
        _locator = 0;
9✔
78
    } else {
79
        _locator += operation * _blockSize;
1✔
80
        _locator = std::max(0, _locator);
1✔
81
    }
82
}
10✔
83

84
void IODeviceTape::doRead() {
2✔
85
    IODeviceStorage::doRead();
2✔
86
    _locator += _blockSize;
2✔
87
}
2✔
88

89
void IODeviceTape::doWrite() {
2✔
90
    IODeviceStorage::doWrite();
2✔
91
    _locator += _blockSize;
2✔
92
}
2✔
93

94
IODeviceDisk::IODeviceDisk(int32_t storageSize) : IODeviceStorage(storageSize) {
9✔
95
    _type = IODeviceType::DISK;
9✔
96
    _readyRate = 0.5;
9✔
97
}
9✔
98

99
void IODeviceDisk::control(int32_t operation) {
9✔
100
    _locator = operation;
9✔
101
}
9✔
102

103
IODeviceSeqReader::IODeviceSeqReader(int32_t storageSize) : IODeviceStorage(storageSize) {
6✔
104
    _allowWrite = false;
6✔
105
}
6✔
106

107
void IODeviceSeqReader::doRead() {
2✔
108
    IODeviceStorage::doRead();
2✔
109
    _locator += _blockSize;
2✔
110
}
2✔
111

112
IODeviceSeqWriter::IODeviceSeqWriter(int32_t storageSize) : IODeviceStorage(storageSize) {
11✔
113
    _allowRead = false;
11✔
114
}
11✔
115

116
void IODeviceSeqWriter::doWrite() {
57✔
117
    IODeviceStorage::doWrite();
57✔
118
    _locator += _blockSize;
57✔
119
}
57✔
120

121
IODeviceCardReader::IODeviceCardReader(int32_t storageSize) : IODeviceSeqReader(storageSize) {
4✔
122
    _type = IODeviceType::CARD_READER;
4✔
123
    _blockSize = 16;
4✔
124
    _readyRate = 0.2;
4✔
125
}
4✔
126

127
IODeviceCardPunch::IODeviceCardPunch(int32_t storageSize) : IODeviceSeqWriter(storageSize) {
9✔
128
    _type = IODeviceType::CARD_PUNCH;
9✔
129
    _blockSize = 16;
9✔
130
    _readyRate = 0.1;
9✔
131
}
9✔
132

133
IODeviceLinePrinter::IODeviceLinePrinter(const int32_t storageSize, const int32_t numLinesPerPage) :
2✔
134
    IODeviceSeqWriter(storageSize), _numLinesPerPage(numLinesPerPage) {
2✔
135
    _type = IODeviceType::LINE_PRINTER;
2✔
136
    _blockSize = 24;
2✔
137
    _readyRate = 0.1;
2✔
138
}
2✔
139

140
void IODeviceLinePrinter::control(int32_t) {
2✔
141
    const auto numWordsPerPage = NUM_WORDS_PER_LINE * _numLinesPerPage;
2✔
142
    const auto currentPage = _locator / numWordsPerPage;
2✔
143
    _locator = (currentPage + 1) * numWordsPerPage;
2✔
144
}
2✔
145

NEW
146
std::string IODeviceLinePrinter::line(const int32_t pageNum, const int32_t lineNum) const {
×
NEW
147
    const int32_t offset = pageNum * NUM_WORDS_PER_LINE * _numLinesPerPage + lineNum * NUM_WORDS_PER_LINE;
×
UNCOV
148
    std::ostringstream out;
×
NEW
149
    for (int i = 0; i < NUM_WORDS_PER_LINE; ++i) {
×
UNCOV
150
        out << _storage[offset + i].getCharacters();
×
151
    }
UNCOV
152
    return out.str();
×
UNCOV
153
}
×
154

155
IODeviceTypewriter::IODeviceTypewriter(int32_t storageSize) : IODeviceSeqReader(storageSize) {
1✔
156
    _type = IODeviceType::TYPEWRITER;
1✔
157
    _blockSize = 14;
1✔
158
    _readyRate = 0.2;
1✔
159
}
1✔
160

161
IODevicePaperTape::IODevicePaperTape(int32_t storageSize) : IODeviceSeqReader(storageSize) {
1✔
162
    _type = IODeviceType::PAPER_TAPE;
1✔
163
    _blockSize = 14;
1✔
164
    _readyRate = 0.2;
1✔
165
}
1✔
166

167
void IODevicePaperTape::control(int32_t) {
1✔
168
    _locator = 0;
1✔
169
}
1✔
170

171
};  // namespace mixal
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc