xref: /aosp_15_r20/external/rappor/tests/compare_dist_test.R (revision 2abb31345f6c95944768b5222a9a5ed3fc68cc00)
1#!/usr/bin/env Rscript
2#
3# Copyright 2014 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17library(RUnit)
18
19source('tests/compare_dist.R')
20
21TestProcessAll <- function() {
22  ctx <- new.env()
23  ctx$actual <- data.frame(string = c('v1', 'v2', 'v3'), proportion = c(0.2, 0.3, 0.5),
24                           count = c(2, 3, 5))
25  ctx$rappor <- data.frame(strings = c('v2', 'v3', 'v4'), proportion = c(0.1, 0.2, 0.3))
26
27  metrics <- CompareRapporVsActual(ctx)$metrics
28  str(metrics)
29
30  # sum of rappor proportions
31  checkEqualsNumeric(0.6, metrics$sum_proportion)
32
33  # v1  v2  v3  v4
34  # 0.2 0.3 0.5 0.0
35  # 0.0 0.1 0.2 0.3
36
37  # (0.2 + 0.2 + 0.3 + 0.3) / 2
38  checkEqualsNumeric(0.5, metrics$total_variation)
39
40  print(metrics$total_variation)
41}
42
43TestProcessAll()
44