Skip to content

Commit

Permalink
Architecture comparators
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Jan 19, 2025
1 parent 433713b commit af31749
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
70 changes: 70 additions & 0 deletions macho/architecture.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { assertEquals } from '@std/assert';
import {
CPU_SUBTYPE_LIB64,
CPU_SUBTYPE_MULTIPLE,
CPU_SUBTYPE_X86_64_H,
CPU_TYPE_I386,
CPU_TYPE_X86_64,
} from '../const.ts';
import { FatArch } from '../mach/fatarch.ts';
import { FatArch64 } from '../mach/fatarch64.ts';
import { Architecture } from './architecture.ts';
Expand Down Expand Up @@ -43,3 +50,66 @@ Deno.test('constructor(archInFile: FatArch64)', () => {
assertEquals(a.cpuSubType(), 0x456789);
assertEquals(a.cpuSubtypeFull(), 0x23456789);
});

Deno.test('bool', () => {
assertEquals(new Architecture(0, 0).bool(), false);
assertEquals(new Architecture(0, 1).bool(), false);
assertEquals(new Architecture(1, 0).bool(), true);
assertEquals(new Architecture(1, 1).bool(), true);
});

Deno.test('equals', () => {
assertEquals(new Architecture(1, 2).equals(new Architecture(1, 2)), true);
assertEquals(new Architecture(1, 2).equals(new Architecture(1, 3)), false);
assertEquals(new Architecture(2, 2).equals(new Architecture(1, 2)), false);
});

Deno.test('lessThan', () => {
assertEquals(
new Architecture(1, 2).lessThan(new Architecture(2, 2)),
true,
);
assertEquals(
new Architecture(1, 2).lessThan(new Architecture(1, 3)),
true,
);
assertEquals(
new Architecture(1, 2).lessThan(new Architecture(1, 2)),
false,
);
assertEquals(
new Architecture(2, 2).lessThan(new Architecture(1, 2)),
false,
);
assertEquals(
new Architecture(1, 2).lessThan(new Architecture(1, 2)),
false,
);
});

Deno.test('matches', () => {
{
const arch = new Architecture(CPU_TYPE_X86_64, CPU_SUBTYPE_MULTIPLE);
const tmpl = new Architecture(CPU_TYPE_I386, CPU_SUBTYPE_MULTIPLE);
assertEquals(arch.matches(tmpl), false);
}
{
const arch = new Architecture(CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_H);
const tmpl = new Architecture(CPU_TYPE_X86_64, CPU_SUBTYPE_MULTIPLE);
assertEquals(arch.matches(tmpl), true);

// Not symmetric.
assertEquals(tmpl.matches(arch), false);
}
{
const arch = new Architecture(
CPU_TYPE_X86_64,
CPU_SUBTYPE_X86_64_H | CPU_SUBTYPE_LIB64,
);
const tmpl = new Architecture(
CPU_TYPE_X86_64,
CPU_SUBTYPE_X86_64_H,
);
assertEquals(arch.matches(tmpl), true);
}
});
47 changes: 47 additions & 0 deletions macho/architecture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,51 @@ export class Architecture {
public cpuSubtypeFull(): number {
return this.second;
}

/**
* Is architecture valid.
*
* @returns Is valid.
*/
public bool(): boolean {
return !!this.first;
}

/**
* If architectures are equal.
*
* @param arch Architecture to compare.
* @returns Is equal.
*/
public equals(arch: Const<Architecture>): boolean {
return this.first === arch.first && this.second === arch.second;
}

/**
* If architecture is less than another.
*
* @param arch Architecture to compare.
* @returns Is less than.
*/
public lessThan(arch: Const<Architecture>): boolean {
const x = this.first;
const y = arch.first;
return x < y || (!(y < x) && this.second < arch.second);
}

/**
* Check is architecture matches template, asymmetric comparison.
*
* @param templ Template architecture.
* @returns Matches template.
*/
public matches(templ: Const<Architecture>): boolean {
if (this.first !== templ.first) {
return false;
}
if (templ.second === CPU_SUBTYPE_MULTIPLE) {
return true;
}
return !((this.second ^ templ.second) & ~CPU_SUBTYPE_MASK);
}
}

0 comments on commit af31749

Please sign in to comment.