Skip to main content

snarkvm_console_program/data/dynamic/future/
equal.rs

1// Copyright (c) 2019-2026 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::*;
17
18impl<N: Network> Eq for DynamicFuture<N> {}
19
20impl<N: Network> PartialEq for DynamicFuture<N> {
21    /// Returns `true` if `self` and `other` are equal.
22    fn eq(&self, other: &Self) -> bool {
23        *self.is_equal(other)
24    }
25}
26
27impl<N: Network> Equal<Self> for DynamicFuture<N> {
28    type Output = Boolean<N>;
29
30    /// Returns `true` if `self` and `other` are equal.
31    fn is_equal(&self, other: &Self) -> Self::Output {
32        self.program_name.is_equal(&other.program_name)
33            & self.program_network.is_equal(&other.program_network)
34            & self.function_name.is_equal(&other.function_name)
35            & self.checksum.is_equal(&other.checksum)
36    }
37
38    /// Returns `true` if `self` and `other` are *not* equal.
39    fn is_not_equal(&self, other: &Self) -> Self::Output {
40        !self.is_equal(other)
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use crate::{Future, Plaintext, ProgramID};
48    use snarkvm_console_network::MainnetV0;
49
50    use core::str::FromStr;
51
52    type CurrentNetwork = MainnetV0;
53
54    /// Helper to create a DynamicFuture from a static future for testing.
55    fn create_dynamic_future(
56        program_id: &str,
57        function_name: &str,
58        arguments: Vec<Argument<CurrentNetwork>>,
59    ) -> DynamicFuture<CurrentNetwork> {
60        let future = Future::new(
61            ProgramID::from_str(program_id).unwrap(),
62            Identifier::from_str(function_name).unwrap(),
63            arguments,
64        );
65        DynamicFuture::from_future(&future).unwrap()
66    }
67
68    #[test]
69    fn test_is_equal() {
70        // Create two identical dynamic futures.
71        let args = vec![Argument::Plaintext(Plaintext::from_str("100u64").unwrap())];
72        let dynamic1 = create_dynamic_future("test.aleo", "foo", args.clone());
73        let dynamic2 = create_dynamic_future("test.aleo", "foo", args);
74
75        // They should be equal.
76        assert!(*dynamic1.is_equal(&dynamic2));
77        assert!(dynamic1 == dynamic2);
78        assert!(!*dynamic1.is_not_equal(&dynamic2));
79    }
80
81    #[test]
82    fn test_is_not_equal_program_name() {
83        // Create dynamic futures with different program names.
84        let args = vec![Argument::Plaintext(Plaintext::from_str("100u64").unwrap())];
85        let dynamic1 = create_dynamic_future("test.aleo", "foo", args.clone());
86        let dynamic2 = create_dynamic_future("other.aleo", "foo", args);
87
88        // They should not be equal.
89        assert!(!*dynamic1.is_equal(&dynamic2));
90        assert!(dynamic1 != dynamic2);
91        assert!(*dynamic1.is_not_equal(&dynamic2));
92    }
93
94    #[test]
95    fn test_is_not_equal_function_name() {
96        // Create dynamic futures with different function names.
97        let args = vec![Argument::Plaintext(Plaintext::from_str("100u64").unwrap())];
98        let dynamic1 = create_dynamic_future("test.aleo", "foo", args.clone());
99        let dynamic2 = create_dynamic_future("test.aleo", "bar", args);
100
101        // They should not be equal.
102        assert!(!*dynamic1.is_equal(&dynamic2));
103        assert!(dynamic1 != dynamic2);
104        assert!(*dynamic1.is_not_equal(&dynamic2));
105    }
106
107    #[test]
108    fn test_is_not_equal_checksum() {
109        // Create dynamic futures with different arguments (thus different checksums).
110        let dynamic1 = create_dynamic_future("test.aleo", "foo", vec![Argument::Plaintext(
111            Plaintext::from_str("100u64").unwrap(),
112        )]);
113        let dynamic2 = create_dynamic_future("test.aleo", "foo", vec![Argument::Plaintext(
114            Plaintext::from_str("200u64").unwrap(),
115        )]);
116
117        // They should not be equal due to different checksums.
118        assert!(!*dynamic1.is_equal(&dynamic2));
119        assert!(dynamic1 != dynamic2);
120        assert!(*dynamic1.is_not_equal(&dynamic2));
121    }
122
123    #[test]
124    fn test_is_equal_empty_arguments() {
125        // Create two identical dynamic futures with empty arguments.
126        let dynamic1 = create_dynamic_future("test.aleo", "foo", vec![]);
127        let dynamic2 = create_dynamic_future("test.aleo", "foo", vec![]);
128
129        // They should be equal.
130        assert!(*dynamic1.is_equal(&dynamic2));
131        assert!(dynamic1 == dynamic2);
132    }
133}