Skip to content

Commit

Permalink
Added unit tests for Swift and Rust of sized lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri6037 committed Aug 21, 2024
1 parent dd2e700 commit 4e16fad
Show file tree
Hide file tree
Showing 12 changed files with 568 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Swift/bp3d-protoc.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
DA7659122C667AB90036DDED /* bit.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7659112C667AB90036DDED /* bit.swift */; };
DA7659142C66A7620036DDED /* byte.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7659132C66A7620036DDED /* byte.swift */; };
DA7659162C66A9710036DDED /* array.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7659152C66A9710036DDED /* array.swift */; };
DA7AAC3B2C76772E00993680 /* lists2.messages_from_slice.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7AAC362C76772E00993680 /* lists2.messages_from_slice.swift */; };
DA7AAC3C2C76772E00993680 /* lists2.messages_write.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7AAC372C76772E00993680 /* lists2.messages_write.swift */; };
DA7AAC3D2C76772E00993680 /* lists2.messages.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7AAC382C76772E00993680 /* lists2.messages.swift */; };
DA7AAC3E2C76772E00993680 /* lists2.structures.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7AAC392C76772E00993680 /* lists2.structures.swift */; };
DA7AAC402C76776A00993680 /* Lists2Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7AAC3F2C76776A00993680 /* Lists2Tests.swift */; };
DA7D6F952C709F7900A176F2 /* values.messages_from_slice.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7D6F902C709F7900A176F2 /* values.messages_from_slice.swift */; };
DA7D6F962C709F7900A176F2 /* values.messages_write.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7D6F912C709F7900A176F2 /* values.messages_write.swift */; };
DA7D6F972C709F7900A176F2 /* values.messages.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7D6F922C709F7900A176F2 /* values.messages.swift */; };
Expand Down Expand Up @@ -150,6 +155,11 @@
DA7659112C667AB90036DDED /* bit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = bit.swift; sourceTree = "<group>"; };
DA7659132C66A7620036DDED /* byte.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = byte.swift; sourceTree = "<group>"; };
DA7659152C66A9710036DDED /* array.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = array.swift; sourceTree = "<group>"; };
DA7AAC362C76772E00993680 /* lists2.messages_from_slice.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = lists2.messages_from_slice.swift; sourceTree = "<group>"; };
DA7AAC372C76772E00993680 /* lists2.messages_write.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = lists2.messages_write.swift; sourceTree = "<group>"; };
DA7AAC382C76772E00993680 /* lists2.messages.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = lists2.messages.swift; sourceTree = "<group>"; };
DA7AAC392C76772E00993680 /* lists2.structures.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = lists2.structures.swift; sourceTree = "<group>"; };
DA7AAC3F2C76776A00993680 /* Lists2Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lists2Tests.swift; sourceTree = "<group>"; };
DA7D6F902C709F7900A176F2 /* values.messages_from_slice.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = values.messages_from_slice.swift; sourceTree = "<group>"; };
DA7D6F912C709F7900A176F2 /* values.messages_write.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = values.messages_write.swift; sourceTree = "<group>"; };
DA7D6F922C709F7900A176F2 /* values.messages.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = values.messages.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -238,6 +248,7 @@
DA7D6FA22C70BEBB00A176F2 /* UnionsTests.swift */,
DA13C7722C710A3100B6D4B1 /* ArraysTests.swift */,
DAFEA9EC2C73C268006FAD47 /* ListsTests.swift */,
DA7AAC3F2C76776A00993680 /* Lists2Tests.swift */,
);
path = testprogTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -324,6 +335,17 @@
path = codec;
sourceTree = "<group>";
};
DA7AAC3A2C76772E00993680 /* lists2 */ = {
isa = PBXGroup;
children = (
DA7AAC362C76772E00993680 /* lists2.messages_from_slice.swift */,
DA7AAC372C76772E00993680 /* lists2.messages_write.swift */,
DA7AAC382C76772E00993680 /* lists2.messages.swift */,
DA7AAC392C76772E00993680 /* lists2.structures.swift */,
);
path = lists2;
sourceTree = "<group>";
};
DA7D6F942C709F7900A176F2 /* values */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -408,6 +430,7 @@
DAC4A9BE2C6E183E00ED8343 /* testprog */ = {
isa = PBXGroup;
children = (
DA7AAC3A2C76772E00993680 /* lists2 */,
DAFEA9E72C73C1F5006FAD47 /* lists */,
DA7D6FA82C70CACA00A176F2 /* arrays */,
DA7D6F9D2C709F8200A176F2 /* unions */,
Expand Down Expand Up @@ -647,6 +670,7 @@
DA7E14D82C6F78AC00FE83FE /* EnumsTests.swift in Sources */,
DA72E66A2C6E91BF008F8F23 /* ViewsTests.swift in Sources */,
DA9DDA6D2C6E1C61000A9C70 /* BitsTests.swift in Sources */,
DA7AAC402C76776A00993680 /* Lists2Tests.swift in Sources */,
DA72E6712C6E96EE008F8F23 /* StructsTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -690,6 +714,7 @@
DA7D6FA12C709F8200A176F2 /* unions.unions.swift in Sources */,
DA72E6682C6E9181008F8F23 /* views.structures.swift in Sources */,
DA7D6F9E2C709F8200A176F2 /* unions.messages_from_slice.swift in Sources */,
DA7AAC3C2C76772E00993680 /* lists2.messages_write.swift in Sources */,
DA72E66D2C6E9330008F8F23 /* structs.structures.swift in Sources */,
DA7E14F72C6FFDF100FE83FE /* unions2.structures.swift in Sources */,
DA7D6F962C709F7900A176F2 /* values.messages_write.swift in Sources */,
Expand All @@ -698,19 +723,22 @@
DA7E14F02C6F88FD00FE83FE /* test.messages.swift in Sources */,
DA72E6742C6E9BFC008F8F23 /* struct_arrays.structures.swift in Sources */,
DA7D6F972C709F7900A176F2 /* values.messages.swift in Sources */,
DA7AAC3B2C76772E00993680 /* lists2.messages_from_slice.swift in Sources */,
DA7E14F12C6F88FD00FE83FE /* test.structures.swift in Sources */,
DA7D6FAB2C70CACA00A176F2 /* arrays.messages.swift in Sources */,
DAFEA9E92C73C1F5006FAD47 /* lists.messages_write.swift in Sources */,
DA7D6FA02C709F8200A176F2 /* unions.messages.swift in Sources */,
DAFEA9E82C73C1F5006FAD47 /* lists.messages_from_slice.swift in Sources */,
DA7D6FAA2C70CACA00A176F2 /* arrays.messages_write.swift in Sources */,
DA7AAC3E2C76772E00993680 /* lists2.structures.swift in Sources */,
DA7E14D62C6F4D4300FE83FE /* enums.structures.swift in Sources */,
DAFEA9EB2C73C1F5006FAD47 /* lists.structures.swift in Sources */,
DA7D6FA92C70CACA00A176F2 /* arrays.messages_from_slice.swift in Sources */,
DA7E14F82C6FFDF100FE83FE /* unions2.unions.swift in Sources */,
DA7E14F62C6FFDF100FE83FE /* unions2.enums.swift in Sources */,
DA7D6FAC2C70CACA00A176F2 /* arrays.structures.swift in Sources */,
DA72E6652C6E5E37008F8F23 /* bits2.structures.swift in Sources */,
DA7AAC3D2C76772E00993680 /* lists2.messages.swift in Sources */,
DA7D6F9F2C709F8200A176F2 /* unions.messages_write.swift in Sources */,
DA7D6F952C709F7900A176F2 /* values.messages_from_slice.swift in Sources */,
);
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion Swift/testprog/generate_proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
../../target/debug/protoc --header ../../LICENSE.txt -g swift ../../testprog/src/values.json5
../../target/debug/protoc --header ../../LICENSE.txt -i ../../testprog/src/enums.json5 Self -i ../../testprog/src/values.json5 Self -s swift -g swift ../../testprog/src/unions.json5
../../target/debug/protoc --header ../../LICENSE.txt -g swift ../../testprog/src/arrays.json5
../../target/debug/protoc --header ../../LICENSE.txt -i ../../testprog/src/enums.json5 Self -i ../../testprog/src/values.json5 Self -i ../../testprog/src/unions.json5 Self -s swift -g swift ../../testprog/src/lists.json5
../../target/debug/protoc --header ../../LICENSE.txt -i ../../testprog/src/enums.json5 Self -i ../../testprog/src/values.json5 Self -i ../../testprog/src/unions.json5 Self -s swift -g swift ../../testprog/src/lists.json5 ../../testprog/src/lists2.json5
56 changes: 56 additions & 0 deletions Swift/testprog/lists2/lists2.messages.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024, BlockProject 3D
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of BlockProject 3D nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import Foundation;
import BP3DProto;

public struct Lists2SpanRun<B: BP3DProto.Buffer> {
public let times: Lists2Times<B>;
public let vars: Lists2SpanRunVars<B>;

public init(times: Lists2Times<B>, vars: Lists2SpanRunVars<B>) {
self.times = times;
self.vars = vars;

}

}

public typealias Lists2SpanRunVars<B: BP3DProto.Buffer> = BP3DProto.List<B, BP3DProto.ValueLE<B, UInt8>, UnionsItem<B>>;

public struct Lists2Dataset<B: BP3DProto.Buffer> {
public let runs: Lists2DatasetRuns<B>;

public init(runs: Lists2DatasetRuns<B>) {
self.runs = runs;

}

}

public typealias Lists2DatasetRuns<B: BP3DProto.Buffer> = BP3DProto.List<B, BP3DProto.ValueLE<B, UInt32>, Lists2SpanRun<B>>;
66 changes: 66 additions & 0 deletions Swift/testprog/lists2/lists2.messages_from_slice.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2024, BlockProject 3D
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of BlockProject 3D nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import Foundation;
import BP3DProto;

extension Lists2SpanRun: BP3DProto.FromSlice {
public typealias Buffer = B;
public typealias Output = Self;
public static func from(slice: B) throws -> BP3DProto.Message<Self> {
var byteOffset = 0;
let timesMsg = try Lists2Times.from(slice: slice[byteOffset...]);
byteOffset += timesMsg.size;
let times = timesMsg.data;
let varsMsg = try BP3DProto.SizedList<B, BP3DProto.ValueLE<B, UInt8>, BP3DProto.ValueLE<B, UInt16>, UnionsItem<B>>.from(slice: slice[byteOffset...]);
byteOffset += varsMsg.size;
let vars = varsMsg.data;

let _data = Lists2SpanRun(
times: times,
vars: vars
);
return BP3DProto.Message(size: byteOffset, data: _data);
}
}

extension Lists2Dataset: BP3DProto.FromSlice {
public typealias Buffer = B;
public typealias Output = Self;
public static func from(slice: B) throws -> BP3DProto.Message<Self> {
var byteOffset = 0;
let runsMsg = try BP3DProto.UnsizedList<B, BP3DProto.ValueLE<B, UInt32>, Lists2SpanRun<B>>.from(slice: slice[byteOffset...]);
byteOffset += runsMsg.size;
let runs = runsMsg.data;

let _data = Lists2Dataset(
runs: runs
);
return BP3DProto.Message(size: byteOffset, data: _data);
}
}
47 changes: 47 additions & 0 deletions Swift/testprog/lists2/lists2.messages_write.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2024, BlockProject 3D
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of BlockProject 3D nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import Foundation;
import BP3DProto;

extension Lists2SpanRun: BP3DProto.WriteTo {
public typealias Input = Self;
public static func write<B1: BP3DProto.WritableBuffer>(input: Self, to out: inout B1) throws {
try Lists2Times.write(input: input.times, to: &out);
try BP3DProto.SizedList<B, BP3DProto.ValueLE<B, UInt8>, BP3DProto.ValueLE<B, UInt16>, UnionsItem<B>>.write(input: input.vars, to: &out);

}
}

extension Lists2Dataset: BP3DProto.WriteTo {
public typealias Input = Self;
public static func write<B1: BP3DProto.WritableBuffer>(input: Self, to out: inout B1) throws {
try BP3DProto.List<B, BP3DProto.ValueLE<B, UInt32>, Lists2SpanRun<B>>.write(input: input.runs, to: &out);

}
}
101 changes: 101 additions & 0 deletions Swift/testprog/lists2/lists2.structures.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) 2024, BlockProject 3D
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of BlockProject 3D nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import Foundation;
import BP3DProto;

public struct Lists2Times<T>: BP3DProto.FixedSize, FromBuffer {
public typealias Buffer = T
var data: T
public static var size: Int { 16 }
public init(_ data: T) {
self.data = data;
}
}
extension Lists2Times<BP3DProto.DataBuffer> {
public init() {
self.data = BP3DProto.DataBuffer(size: 16)
}
}
public let SIZE_LISTS2_TIMES: Int = 16;
extension Lists2Times: BP3DProto.WriteTo where T: BP3DProto.Buffer {
public typealias Input = Lists2Times;
public static func write<B: BP3DProto.WritableBuffer>(input: Input, to out: inout B) throws {
out.write(bytes: input.data[...16].toData());
}
}
extension Lists2Times: BP3DProto.FromSlice where T: BP3DProto.Buffer {
public typealias Buffer = T;
public typealias Output = Lists2Times;
public static func from(slice: T) throws -> BP3DProto.Message<Output> {
if slice.size < 16 {
throw BP3DProto.Error.truncated;
}
return BP3DProto.Message(size: 16, data: Lists2Times(slice[...16]));
}
}
extension Lists2Times where T: BP3DProto.Buffer {
public var rawStart: UInt64 {
BP3DProto.ByteCodecLE.readAligned(UInt64.self, self.data[0...8])

}
public var start: UInt64 {
self.rawStart
}
public var rawEnd: UInt64 {
BP3DProto.ByteCodecLE.readAligned(UInt64.self, self.data[8...16])

}
public var end: UInt64 {
self.rawEnd
}

}
extension Lists2Times where T: BP3DProto.Buffer, T: BP3DProto.WritableBuffer {
public func setRawStart(_ value: UInt64) {
var buffer = self.data[0...8];
BP3DProto.ByteCodecLE.writeAligned(UInt64.self, &buffer, value: value);

}
@discardableResult
public func setStart(_ value: UInt64) -> Self {
self.setRawStart(value);
return self;
}
public func setRawEnd(_ value: UInt64) {
var buffer = self.data[8...16];
BP3DProto.ByteCodecLE.writeAligned(UInt64.self, &buffer, value: value);

}
@discardableResult
public func setEnd(_ value: UInt64) -> Self {
self.setRawEnd(value);
return self;
}

}
Loading

0 comments on commit 4e16fad

Please sign in to comment.