Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Fix tinyv2 generator & MappingUtil.split
Use lists again, but using sets on `equals`
More equals/hashCode/toString
  • Loading branch information
XiaoPangxie732 committed Jan 31, 2025
1 parent 1dce54c commit bed0c42
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void testFormatExtension() {

@Test
void testProcessorAndGenerator(@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path tmp) throws IOException {
LOGGER.info(tmp.toString());
// LOGGER.info(tmp.toString());
var is = getClass().getClassLoader().getResourceAsStream("parchment.json");
assertNotNull(is);
var c1 = ParchmentMappingFormat.INSTANCE.read(is);
Expand All @@ -66,14 +66,12 @@ void testProcessorAndGenerator(@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path t
}
try (var reader = Files.newBufferedReader(path)) {
var c2 = ParchmentMappingFormat.INSTANCE.read(reader);
// assertEquals(c1, c2);
LOGGER.info("c1 == c2: {}", c1.equals(c2));
assertEquals(c1, c2);
}
try (var reader = Files.newBufferedReader(path)) {
ObjectArrayList<String> lines = reader.lines().collect(ObjectArrayList.toList());
var c3 = ParchmentMappingProcessor.INSTANCE.process(lines);
// assertEquals(c1, c3);
LOGGER.info("c1 == c3: {}", c1.equals(c3));
assertEquals(c1, c3);
} catch (IOException e) {
throw Utils.wrapInRuntime(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import cn.maxpixel.mcdecompiler.mapping.component.Descriptor;
import cn.maxpixel.mcdecompiler.mapping.component.Owned;
import cn.maxpixel.mcdecompiler.mapping.util.DescriptorRemapper;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectList;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

Expand All @@ -44,10 +45,9 @@ public class ClassMapping<T extends Mapping> {
* The mapping for this class
*/
public T mapping;
// private final ObjectArrayList<@NotNull T> methods = new ObjectArrayList<>();// TODO: Remove this after passing the tests
// private final ObjectArrayList<@NotNull T> fields = new ObjectArrayList<>();
private final ObjectLinkedOpenHashSet<@NotNull T> methods = new ObjectLinkedOpenHashSet<>();
private final ObjectLinkedOpenHashSet<@NotNull T> fields = new ObjectLinkedOpenHashSet<>();
private final ObjectArrayList<@NotNull T> methods = new ObjectArrayList<>();
private final ObjectArrayList<@NotNull T> fields = new ObjectArrayList<>();
private final ObjectOpenHashSet<@NotNull T> memberSet = new ObjectOpenHashSet<>();

/**
* No-arg constructor
Expand Down Expand Up @@ -143,8 +143,7 @@ public ClassMapping<T> addMethod(T method) {
* @apiNote You shouldn't add methods through this list
* @return The methods
*/
// public ObjectList<T> getMethods() {
public ObjectSet<T> getMethods() {// TODO: Remove this after passing the tests
public ObjectList<T> getMethods() {
return methods;
}

Expand All @@ -154,8 +153,7 @@ public ObjectSet<T> getMethods() {// TODO: Remove this after passing the tests
* @apiNote You shouldn't add methods through this list
* @return The fields
*/
// public ObjectList<T> getFields() {
public ObjectSet<T> getFields() {// TODO: Remove this after passing the tests
public ObjectList<T> getFields() {
return fields;
}

Expand Down Expand Up @@ -215,42 +213,27 @@ public static boolean mappingEquals(ClassMapping<?> a, ClassMapping<?> b) {
return (a == b) || (a != null && b != null && Objects.equals(a.mapping, b.mapping));
}

/* Auto-generated equals, hashCode and toString methods */
private ObjectOpenHashSet<@NotNull T> populateMemberSet() {
if (!memberSet.containsAll(methods) || !memberSet.containsAll(fields)) {// TODO: Profile to see whether this condition is needed
memberSet.clear();
memberSet.addAll(methods);
memberSet.addAll(fields);
}
return memberSet;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ClassMapping<?> that)) return false;
boolean res = Objects.equals(mapping, that.mapping) && methods.equals(that.methods) && fields.equals(that.fields);
if (!res && Objects.equals(mapping, that.mapping)) {
System.err.println("Not equals(method=" + methods.equals(that.methods) + ",field=" + fields.equals(that.fields) + "): this: <" + this + "> o: <" + o + ">");
for (@NotNull T method : methods) {
if (!that.methods.contains(method)) {
System.err.println("Not contains: a<" + method + ">");
}
}
System.err.println(methods.first().equals(that.methods.first()));
System.err.println(that.methods.first().equals(methods.first()));
System.err.println(that.methods.contains(methods.first()));
System.err.println(that.methods.contains(that.methods.first()));
System.err.println(methods.contains(methods.first()));
System.err.println(methods.contains(that.methods.first()));
// that.methods.en
System.err.println(methods.first().hashCode());
System.err.println(that.methods.first().hashCode());
System.err.println(methods.contains(methods.first()));
System.err.println(that.methods.contains(that.methods.first()));
ObjectLinkedOpenHashSet<Object> set = new ObjectLinkedOpenHashSet<>();
for (@NotNull T method : methods) {
set.add(method);
}
System.err.println(set.contains(that.methods.first()));
}
return res;
return Objects.equals(mapping, that.mapping) && methods.size() == that.methods.size() &&
fields.size() == that.fields.size() && populateMemberSet().containsAll(that.fields) &&
memberSet.containsAll(that.methods);
}

@Override
public int hashCode() {
return Objects.hash(mapping, methods, fields);
return Objects.hash(mapping, populateMemberSet());
}

@Override
Expand All @@ -259,6 +242,6 @@ public String toString() {
"mapping=" + mapping +
", methods=" + methods +
", fields=" + fields +
"} \n";
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
import cn.maxpixel.mcdecompiler.mapping.trait.MappingTrait;
import cn.maxpixel.mcdecompiler.mapping.trait.NamespacedTrait;
import cn.maxpixel.mcdecompiler.mapping.util.DescriptorRemapper;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
* A collection of mappings of classes and packages.
* <p>
Expand All @@ -45,8 +44,8 @@ public class ClassifiedMapping<T extends Mapping> extends MappingCollection<T> {
/**
* Classes of this mapping.
*/
// public final ObjectArrayList<@NotNull ClassMapping<@NotNull T>> classes = new ObjectArrayList<>();// TODO: Remove this after passing the tests
public final ObjectLinkedOpenHashSet<@NotNull ClassMapping<@NotNull T>> classes = new ObjectLinkedOpenHashSet<>();
public final ObjectArrayList<@NotNull ClassMapping<@NotNull T>> classes = new ObjectArrayList<>();
private final ObjectOpenHashSet<@NotNull ClassMapping<@NotNull T>> classSet = new ObjectOpenHashSet<>();

public ClassifiedMapping() {}

Expand Down Expand Up @@ -180,24 +179,30 @@ public static String getSourceNamespace(@NotNull ClassifiedMapping<NamespacedMap
return mappings.getTrait(NamespacedTrait.class).getUnmappedNamespace();
}

/* Auto-generated equals, hashCode and toString methods */
private ObjectOpenHashSet<@NotNull ClassMapping<@NotNull T>> populateClassSet() {
if (!classSet.containsAll(classes)) {// TODO: Profile to see whether this condition is needed
classSet.clear();
classSet.addAll(classes);
}
return classSet;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ClassifiedMapping<?> that)) return false;
return Objects.equals(classes, that.classes) && Objects.equals(packages, that.packages);
if (!super.equals(o)) return false;
return classes.size() == that.classes.size() && populateClassSet().containsAll(that.classes);
}

@Override
public int hashCode() {
return Objects.hash(classes, packages);
return 31 * super.hashCode() + populateClassSet().hashCode();
}

@Override
public String toString() {
return "ClassifiedMapping{" +
"classes=" + classes +
", packages=" + packages +
'}';
"} " + super.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import cn.maxpixel.mcdecompiler.mapping.Mapping;
import cn.maxpixel.mcdecompiler.mapping.trait.MappingTrait;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectCollection;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand All @@ -37,10 +38,10 @@ public abstract class MappingCollection<M extends Mapping> {
/**
* Packages of this mapping.
*/
// public final ObjectArrayList<@NotNull M> packages = new ObjectArrayList<>();// TODO: Remove this after passing the tests
public final ObjectLinkedOpenHashSet<@NotNull M> packages = new ObjectLinkedOpenHashSet<>();
public final ObjectArrayList<@NotNull M> packages = new ObjectArrayList<>();

private final Object2ObjectOpenHashMap<@NotNull Class<? extends MappingTrait>, @NotNull MappingTrait> traits = new Object2ObjectOpenHashMap<>();
private final ObjectOpenHashSet<@NotNull M> packageSet = new ObjectOpenHashSet<>();

/**
* No-arg constructor
Expand Down Expand Up @@ -137,4 +138,32 @@ public final void updateCollection() {
public void clear() {
packages.clear();
}

private ObjectOpenHashSet<@NotNull M> populatePackageSet() {
if (!packageSet.containsAll(packages)) {// TODO: Profile to see whether this condition is needed
packageSet.clear();
packageSet.addAll(packages);
}
return packageSet;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof MappingCollection<?> that)) return false;
return Objects.equals(traits, that.traits) && packages.size() == that.packages.size() &&
populatePackageSet().containsAll(that.packages);
}

@Override
public int hashCode() {
return Objects.hash(traits, populatePackageSet());
}

@Override
public String toString() {
return "MappingCollection{" +
"traits=" + traits +
", packages=" + packages +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ public ObjectList<String> generate(ClassifiedMapping<NamespacedMapping> mappings
lines.add("tiny\t2\t0\t" + String.join("\t", namespaces));
for (ClassMapping<NamespacedMapping> cls : mappings.classes) {
lines.add("c\t" + NamingUtil.concatNamespaces(namespaces, cls.mapping::getName, "\t"));
var classDoc = cls.mapping.getComponent(Documented.class);
if (classDoc != null) {
String content = classDoc.getContentString();
if (!content.isBlank()) lines.add("\tc\t" + TinyUtil.escape(content));
}
cls.getFields().parallelStream().forEach(field -> {
String desc = MappingUtil.Namespaced.checkTiny(namespace0, cls, field);
synchronized (lines) {
lines.add("\tf\t" + desc + '\t' + NamingUtil.concatNamespaces(namespaces, field::getName, "\t"));
if (field.hasComponent(Documented.class)) {
String doc = field.getComponent(Documented.class).getContentString();
var fieldDoc = field.getComponent(Documented.class);
if (fieldDoc != null) {
String doc = fieldDoc.getContentString();
if (!doc.isBlank()) lines.add("\t\tc\t" + TinyUtil.escape(doc));
}
}
Expand All @@ -65,8 +71,9 @@ public ObjectList<String> generate(ClassifiedMapping<NamespacedMapping> mappings
String desc = MappingUtil.Namespaced.checkTiny(namespace0, cls, method);
synchronized (lines) {
lines.add("\tm\t" + desc + '\t' + NamingUtil.concatNamespaces(namespaces, method::getName, "\t"));
if (method.hasComponent(Documented.class)) {
String doc = method.getComponent(Documented.class).getContentString();
var methodDoc = method.getComponent(Documented.class);
if (methodDoc != null) {
String doc = methodDoc.getContentString();
if (!doc.isBlank()) lines.add("\t\tc\t" + TinyUtil.escape(doc));
}
if (method.hasComponent(LocalVariableTable.Namespaced.class)) {
Expand All @@ -81,8 +88,9 @@ public ObjectList<String> generate(ClassifiedMapping<NamespacedMapping> mappings
return name;
}, "\t");
lines.add("\t\tp\t" + index + '\t' + names);
if (localVariable.hasComponent(Documented.class)) {
String doc = localVariable.getComponent(Documented.class).getContentString();
var paramDoc = localVariable.getComponent(Documented.class);
if (paramDoc != null) {
String doc = paramDoc.getContentString();
if (!doc.isBlank()) lines.add("\t\t\tc\t" + TinyUtil.escape(doc));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public interface MappingProcessor<T extends Mapping, C extends MappingCollection
* @return processed mapping collection
*/
C process(ObjectList<String>... contents);// TODO: better ways of merging mapping collections?
// TODO: Maybe use Map<String, List<String>>

interface Unique<T extends Mapping> extends MappingProcessor<T, UniqueMapping<T>> {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import cn.maxpixel.mcdecompiler.mapping.trait.NamespacedTrait;
import cn.maxpixel.mcdecompiler.mapping.util.DescriptorRemapper;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import it.unimi.dsi.fastutil.objects.ObjectList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -176,7 +176,7 @@ else if (mapping.hasComponent(Descriptor.Namespaced.class))
}

public static <T extends Mapping> Object2ObjectOpenHashMap<String, Object2ObjectOpenHashMap<String, T>> genFieldsByUnmappedNameMap(
ObjectSet<ClassMapping<T>> mapping) {
ObjectList<ClassMapping<T>> mapping) {
return mapping.parallelStream().collect(Collectors.toMap(
cm -> cm.mapping.getUnmappedName(),
cm -> cm.getFields().parallelStream().collect(Collectors.toMap(NameGetter::getUnmappedName, Function.identity(),
Expand All @@ -185,19 +185,19 @@ public static <T extends Mapping> Object2ObjectOpenHashMap<String, Object2Object
}

public static <T extends Mapping> Object2ObjectOpenHashMap<String, ClassMapping<T>> genMappingsByUnmappedNameMap(
ObjectSet<ClassMapping<T>> mapping) {
ObjectList<ClassMapping<T>> mapping) {
return mapping.parallelStream().collect(Collectors.toMap(cm -> cm.mapping.getUnmappedName(),
Function.identity(), Utils::onKeyDuplicate, Object2ObjectOpenHashMap::new));
}

public static <T extends Mapping> Object2ObjectOpenHashMap<String, ClassMapping<T>> genMappingsByMappedNameMap(
ObjectSet<ClassMapping<T>> mapping) {
ObjectList<ClassMapping<T>> mapping) {
return mapping.parallelStream().collect(Collectors.toMap(cm -> cm.mapping.getMappedName(),
Function.identity(), Utils::onKeyDuplicate, Object2ObjectOpenHashMap::new));
}

public static Object2ObjectOpenHashMap<String, ClassMapping<NamespacedMapping>> genMappingsByNamespaceMap(
ObjectSet<ClassMapping<NamespacedMapping>> mapping, String namespace) {
ObjectList<ClassMapping<NamespacedMapping>> mapping, String namespace) {
return mapping.parallelStream().collect(Collectors.toMap(m -> m.mapping.getName(namespace),
Function.identity(), Utils::onKeyDuplicate, Object2ObjectOpenHashMap::new));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,24 @@ public void updateCollection(MappingCollection<?> collection) {
}
}
}

@Override
public boolean equals(Object o) {
if (!(o instanceof NamespacedTrait that)) return false;
return Objects.equals(namespaces, that.namespaces) && Objects.equals(mappedNamespace, that.mappedNamespace) && Objects.equals(fallbackNamespace, that.fallbackNamespace);
}

@Override
public int hashCode() {
return Objects.hash(namespaces, mappedNamespace, fallbackNamespace);
}

@Override
public String toString() {
return "NamespacedTrait{" +
"namespaces=" + namespaces +
", mappedNamespace='" + mappedNamespace + '\'' +
", fallbackNamespace='" + fallbackNamespace + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static String[] split(String s, char c) {

public static String[] split(String s, char c, int beginIndex) {// TODO: Do we need an withMapping version?
int i = s.indexOf(c, beginIndex);
if (i == -1) return new String[] {s};
if (i == -1) return new String[] { s.substring(beginIndex) };
int n = 2;
while ((i = s.indexOf(c, i + 1)) != -1) n++;

Expand Down
Loading

0 comments on commit bed0c42

Please sign in to comment.